Ngo Van
Ngo Van

Reputation: 887

Android JsonArray Parse

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

Upvotes: 1

Views: 99

Answers (3)

Raghunandan
Raghunandan

Reputation: 133560

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");
 }

Upvotes: 3

Chirag Ghori
Chirag Ghori

Reputation: 4231

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();
    }

Upvotes: 1

Shad
Shad

Reputation: 1595

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

Upvotes: 1

Related Questions