Thursday, 2 February 2017

java - JSON Parsing in Android




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?


Answer



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...........


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...