Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON Parsing in Android [duplicate]

I have searched alot on JSON Parsing in Android, but couldn't quite convinced. Actually got a brief idea but not so clear yet regarding JSON Parsing.

How to implement the JSON Parsing in the Application?

like image 360
David Brown Avatar asked Sep 14 '10 06:09

David Brown


People also ask

Can JSON have duplicate fields?

The JSON standard recommends that a JSON object not have duplicate field names.

Does JSON support duplicate keys?

JSON with duplicate key entries have to be handled either by ignoring the duplicate entries or by throwing an exception. Until Mule Runtimes 3.8. 6/3.9. 0, JSON Schema Validator handles them by retaining the last duplicate entry and not throwing an exception.

What is JSON parsing in Android?

Android supports all the JSON classes such as JSONStringer, JSONObject, JSONArray, and all other forms to parse the JSON data and fetch the required information by the program. JSON's main advantage is that it is a language-independent, and the JSON object will contain data like a key/value pair.

Where does Android store JSON files?

You need to store you json file in assets folder.


1 Answers

This is a very simple JSON String

{"key1":"value1","key2":"value2"}

In order to get values for it use JSONObject like this :

JSONObject json_obj=new JSONObject(your json string);
String value1=json_obj.getString("key1");
String value2=json_obj.getString("key2");

This is a slightly complex json string

[{"key1":"value1","key2":"value2"},{"key1":"value1","key2":"value2"}]

In order to extract values from this use JSONArray

JSONArray jArray=new JSONArray(your json string);
for(int i=0;i<(jArray.length());i++)
{
    JSONObject json_obj=jArray.getJSONObject(i);
    String value1=json_obj.getString("key1");
    String value2=json_obj.getString("key2");
}

Hope this helps a bit...........

like image 128
viv Avatar answered Oct 17 '22 09:10

viv