Android JsonArray Parse

时间:2014-03-19 04:47:39

标签: android json

我有一个像这样的Json:

{
"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}]
}

如何获得2个Json阵列a和b。
请帮帮我。

我试过这个:

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

抛出异常:

org.json.JSONException: Value {"amount":52,"date":"2014\/03\/10","usage":32} at a of type org.json.JSONObject cannot be converted to JSONArray

3 个答案:

答案 0 :(得分:3)

  

at org.json.JSONObject类型无法转换为JSONArray

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

解析

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

同样适用于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");
 }

答案 1 :(得分:1)

这里有一些示例代码。

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

答案 2 :(得分:1)

试试这个

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