Skip to content Skip to sidebar Skip to footer

How To Read Attribute Value In Json Using Android

I want to read json attribute value, my json format like this {'content':{'@attributes' : { 'start_limit' : 'x','end_limit' : 'x','total_records' : 'x'},'item':[{'category':'x','d

Solution 1:

First of all check your JSON String.Change it to

{"content":{"@attributes" : { "start_limit" :"x","end_limit" : "x","total_records" : "x"}},"item":[{"category":"x","distance':"x"}]}} from

{"content":{"@attributes" : { "start_limit" : "x","end_limit" : "x","total_records" : "x"},"item":[{"category":"x","distance":"x"}]}}

In which you left one closing curly brace before "item".

Now below is the code to get value of "total_records"

String jsonString = "{'content':{'@attributes' : { 'start_limit' :'x','end_limit' : 'x','total_records' : 'x'}},'item':[{'category':'x','distance':'x'}]}}";

publicStringgetTotalRecords(String jsonString){
    try {
            JSONObject mainObject = newJSONObject(jsonString);
            JSONObject contentObject = mainObject.getJSONObject("content");
            JSONObject attributesObject = contentObject.getJSONObject("@attributes");
            String totalRecords = attributesObject.getString("total_records");
            Log.i("Total Records", totalRecords);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    return totalRecords;
}

Hope you understand the problem.

Solution 2:

Simply use JSONObject.

JSONObject obj = newJSONObject(src);
String totalRecs = obj.getString("total_records");

Not 100% sure it works, but it is a good example where to start.

Solution 3:

Hope you've already given it a try before asking in SO. If not, here are a few urls (which I googled): http://www.androidcompetencycenter.com/2009/10/json-parsing-in-android/ and http://about-android.blogspot.com/2010/03/androind-json-parser.html

Post a Comment for "How To Read Attribute Value In Json Using Android"