3

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","distance":"x"}]}},

I want to read total_record's value from json attributes. How can I read ? Please help me

Thanks John Rick

3 Answers 3

4

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'}]}}";

public String getTotalRecords(String jsonString){
    try {
            JSONObject mainObject = new JSONObject(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.

Sign up to request clarification or add additional context in comments.

1 Comment

should be accepted as the correct answer! Thanks, was looking for this too :)
3

Simply use JSONObject.

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

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

3 Comments

@Thanks for your reply,can u give some example for get value from total_records from json
You're code assumes total_records is a property of the top level object, which it isn't.
@RoToRa, I haven't written, that is a solution. Just pointed the direction.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.