Android: Parsing JSON data from a String

0 Comments

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

01.public RunewordParser(String data) throws JSONException {
02.  // Parse String "data"
03.  JSONObject json = (JSONObject) new JSONTokener(data).nextValue();
04. 
05.  // Some primitive data types
06.  stones = json.getString("stones");
07.  matched = json.getInt("matched");
08.  onlyComplete = json.getBoolean("only_complete");
09. 
10.  // Dealing with arrays
11.  JSONArray runewords = json.getJSONArray("runewords");
12.  for (int i = 0; i < runewords.length(); i++) {
13.    Runeword runeword = new Runeword(runewords.getJSONObject(i));
14.    this.runewords.add(runeword);
15.  }
16.}

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