Can anyone send me link which has an example for parsing json in android with a detailed description?
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.
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());
}
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.
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://www.softwaremonkey.org/Code/JsonParser
http://xstream.codehaus.org/json-tutorial.html