Android: Parsing JSON data from a String

2 comments

A very handy built-in class to have is JSONTokener which I used for my Diablo II Runewords app.

public RunewordParser(String data) throws JSONException {
// Parse String "data"
JSONObject json = (JSONObject) new JSONTokener(data).nextValue();

// Some primitive data types
stones = json.getString("stones");
matched = json.getInt("matched");
onlyComplete = json.getBoolean("only_complete");

// Dealing with arrays
JSONArray runewords = json.getJSONArray("runewords");
for (int i = 0; i < runewords.length(); i++) {
Runeword runeword = new Runeword(runewords.getJSONObject(i));
this.runewords.add(runeword);
}
}

That should be enough to get you started. If you've got any questions, leave a comment.

Source

2 comments:

  1. Amazing dude I was searching for something like that, but I'm troubles to fix this:
    I have the following valid json response got from my java webservice:
    ************************************************
    {
    "auctions": [
    {
    "id": 13,
    "name": "gadjet's auction",
    "products": [
    {
    "description": "20639",
    "id": 14,
    "biggerBid": {
    "value": 180
    },
    "name": "soapdish"
    },
    {
    "description": "20640",
    "id": 15,
    "biggerBid": null,
    "name": "Belt"
    }
    ]
    }
    ]
    }
    ************************************************

    I just want to get this json response through it's own regular url such as http://auction-server.herokuapp.com/auctions.json
    and fill a listView with the incoming json's parsed Data.
    If you could helpe me Too much appreciated, best regards!

    ReplyDelete
  2. Hi Marley,

    I'm not sure where you're having issues, but this post may help you with the Listview stuff.

    http://twigstechtips.blogspot.com/2012/01/android-displaying-items-in-listview.html

    ReplyDelete

Leave your thoughts ...
---
If you are having trouble with copy/pasting in comments, you need to sign in or click 'Preview'. For more information about this Firefox bug, see here.

 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog