Android: Parsing JSON data from a String

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

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