Skip to content Skip to sidebar Skip to footer

How To Parse This Json Obj Android

i m facing prob in parsing this obj { 'id': 1909, 'permalink': 'http:some url', 'title': 'Voting begins for third phase of Bihar polls', 'excer

Solution 1:

Lets say "jsonString" is equal to that example string

JSONObject json = newJSONObject(jsonString);
int id = json.getInt("id");
String permalink = json.getString("permalink");
JSONArray tags = json.getJSONArray("tags");
String firstTag = tags.getString(0);

You need to catch JSONExceptions and optionally check json.has("someproperty") before grabbing data.

Solution 2:

u can use Gson gson = new Gson(); List mylist = gson.fromJson(json, listtype);

u have to import gson jar which u can google it

Solution 3:

In android Json classes are available, so no need to go elsewhere...

Step 1 : Init Json object with source string

JSONObject jObject = newJSONObject(srcString);

Step 2 : Init parent tag with another json object if parent tag contains array the take JosnArray else JsonObject, in ur case suppose obj is parent tag then

JSONObject data = jObject.getJSONObject("obj");

Step 3 : Now get String values

   data.getString("id");
    Orif array then






 JSONArray dataArray = data.getJSONArray("tags");
                      JSONObject menuObject =dataArray.getJSONObject(0);                                      
 String firstvalue=       menuObject.getString("first");

Solution 4:

Use the JSONObject and its methods as Ian says above, however if you don't want your app to throw exceptions if any of the values are missing you can also use the 'opt' (Optional) methods, e.g.

JSONObject json = newJSONObject(jsonString);
String permalink = json.optString("permalink","");

Rather than throw an exception if 'permalink' is not found, it will return the second parameter, in this case an empty string.

Post a Comment for "How To Parse This Json Obj Android"