1

I hava a Json like this:

{
"a_num":56.2,
"a_meter":"GM54",
"b_num":26.2,
"b_meter":"WM21",
"a":[
    {"date":"2014\/03\/10","amount":52,"usage":32},
    {"date":"2014\/02\/11","amount":12,"usage":84},
    {"date":"2014\/01\/11","amount":142,"usage":23},
    {"date":"2014\/08\/11","amount":78,"usage":34}],
"b":[
    {"date":"2014\/07\/13","amount":511,"usage":322},
    {"date":"2014\/02\/10","amount":111,"usage":22},
    {"date":"2014\/03\/30","amount":311,"usage":332},
    {"date":"2014\/01\/30","amount":51,"usage":32}]
}

How can I get 2 JsonArrays a and b.
Please help me with this.

I've tried this:

String aNum = json.getString("a_num");  
String aMeter = json.getString("a_meter");  
String bNum = json.getString("b_num");  
String bMeter = json.getString("b_meter"); 
JSONArray listA = json.getJSONArray("a"); 
JSONArray listB = json.getJSONArray("b"); 

It throw exception:

org.json.JSONException: Value {"amount":52,"date":"2014\/03\/10","usage":32} at a of type org.json.JSONObject cannot be converted to JSONArray
2
  • JSONArray listA = json.getJSONArray("a"); this is alright. Do you use listA further in your code. If so post the same Commented Mar 19, 2014 at 4:58
  • Why don't you try it through Gson parsing?, Gson parsing is one of the best JSON parsing I have ever experienced. Commented Mar 19, 2014 at 5:11

3 Answers 3

3

at a of type org.json.JSONObject cannot be converted to JSONArray

"a":[ // jsonArray a
    {     // json object node
    "date":"2014\/03\/10",
    "amount":52,
    "usage":32
    },

To parse

 JSONArray listA = json.getJSONArray("a");
 for(int i=0;i<listA.length();i++)
 {
 JSONObject jb =(JSONObject) liastA.get(i);
 String date = jb.getString("date");
 String amount = jb.getString("amount");
 String usage = jb.getString("usage");
 }

Similarly for JSONArray b

 JSONArray listB = json.getJSONArray("b");
 for(int i=0;i<listB.length();i++)
 {
 JSONObject jb =(JSONObject) liastB.get(i);
 String date = jb.getString("date");
 String amount = jb.getString("amount");
 String usage = jb.getString("usage");
 }
Sign up to request clarification or add additional context in comments.

1 Comment

My fault. The problem is in another code segment (get wrong JsonObject). But, your answer is absolute right. Thanks.
1

Here some sample code.

JSONArray nameArray = resultjJsonObject.names(); //resultjJsonObject is the final object you want to parse
JSONArray aJsonArray = resultjJsonObject.getJSONArray(nameArray.getString(4)); //JsonArrays a
JSONArray bJsonArray = resultjJsonObject.getJSONArray(nameArray.getString(5)); //JsonArrays b

Comments

1

Try this

    JSONObject jsonObject = null;
    try {
        jsonObject = new JSONObject(RESPONSE_STRING);  // set your response string here

        String aNum = jsonObject.getString("a_num");
        String aMeter = jsonObject.getString("a_meter");
        String bNum = jsonObject.getString("b_num");
        String bMeter = jsonObject.getString("b_meter");
        JSONArray listA = jsonObject.getJSONArray("a");
        JSONArray listB = jsonObject.getJSONArray("b");

        for (int i = 0; i < listA.length(); i++) {

            JSONObject jsonObject1 = listA.getJSONObject(i);
            String amount = jsonObject1.getString("amount");
            String date = jsonObject1.getString("date");
            String usage = jsonObject1.getString("usage");

        }

        for (int i = 0; i < listB.length(); i++) {

            JSONObject jsonObject1 = listB.getJSONObject(i);
            String amount = jsonObject1.getString("amount");
            String date = jsonObject1.getString("date");
            String usage = jsonObject1.getString("usage");

        }

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

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.