tags:

views:

287

answers:

5

Can anyone send me link which has an example for parsing json in android with a detailed description?

+1  A: 

Parsing JSON in Java has already been done. I recomend using the Java JSON library that is posted on the JSON website.

I have used it a bunch and it is super simple and works great for me.

This will do all of the hard parsing work for you, so you can just use the data.

jjnguy
Also remember that Android has its own JSON classes. Check package org.json http://developer.android.com/reference/org/json/package-summary.html
klez
A: 

Here's a quick example of how I would parse an array of JSON strings

Suppose your JSON looks like such:

[{"id":1,"delivery_id":1234},{"id":2,"delivery_id":34523},{"id":3,"delivery_id":9873}]

Here would be a quick function to build an Array of a objects we'll call "Messages" from the array of JSON messages:

      ArrayList<Messages> result = new ArrayList<Messages>();

      try {
          // this will break the JSON messages into an array
          JSONArray aryJSONStrings = new JSONArray(json);
          // loop through the array
          for (int i=0; i<aryJSONStrings.length(); i++) {
              Messages msgSum = new Messages();
              msgSum.setMessageId(aryJSONStrings.getJSONObject(i).getInt("id"));
              msgSum.setDeliveryId(aryJSONStrings.getJSONObject(i).getInt("delivery_id"));
              result.add(msgSum);
          }
       }
       catch (JSONException e) {
           Log.e("json", e.toString());
       }
Andrew
A: 

I wrote a blog post a few months ago about building a simple http client dealing with JSON objects.

I also published my source code on GitHub.

Hope that helps.

znq
A: 

There are also these libraries. I prefer Xstream personally because it hasn't ever crashed my app... I tried many of the others and occasionally got uncaught exceptions in the libraries.

http://code.google.com/p/google-gson/

http://jackson.codehaus.org/

http://www.softwaremonkey.org/Code/JsonParser

http://xstream.codehaus.org/json-tutorial.html

http://json-lib.sourceforge.net/

http://flexjson.sourceforge.net/

androidworkz
oops... I use Xstream for XML parsing... sorry. I have been coding since 7am this morning and it's 9:40pm my time.
androidworkz