2017-04-25 68 views
-4

如何获取json数组的长度? 只需要知道response.lenght()。但是这种方法给出错误的数字。请帮助我。Android使用gson解析json数组如何获取json数组的长度

例外: - > org.json.JSONException:不是原始数组:类org.json.JSONArray

,这里是我的代码

private ArrayList<Order> parseJson(JSONArray response){ 
     System.out.println("lenght:" + response.length()); 
     ArrayList<Order> orders = new ArrayList<>(); 
     try { 
      JSONArray jsonarray = new JSONArray(response); 
      for (int i = 0; i < jsonarray.length(); i++) { 
       Order order = new Order(); 
       try { 
        JSONObject rootJsonObject = response.getJSONObject(i); 
        order.setId(rootJsonObject.getInt("id"));      
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

     return orders; 
    } 

[ 
    { 
     "id": 45, 
     "products": [ 
      [ 
       1, 
       10 
      ], 
      [ 
       2, 
       5 
      ], 
      [ 
       3, 
       1 
      ] 
     ], 
     "delivery_price": 18.86,    
     "product_details": [ 
      { 
       "id": 1, 
       "name": "Ürün 1", 
       "price": "4.56", 
       "created_at": "1491491836", 
       "updated_at": "1491491836", 
       "pivot": { 
        "order_id": 45, 
        "product_id": 1 
       } 
      }, 
      { 
       "id": 2, 
       "name": "Ürün 2", 
       "price": "15.76", 
       "created_at": "1491491842", 
       "updated_at": "1491491842", 
       "pivot": { 
        "order_id": 45, 
        "product_id": 2 
       } 
      }, 
      { 
       "id": 3, 
       "name": "Ürün 3", 
       "price": "11.28", 
       "created_at": "1491491849", 
       "updated_at": "1491491849", 
       "pivot": { 
        "order_id": 45, 
        "product_id": 3 
       } 
      } 
     ] 
    }, 
    { 
     "id": 47, 
     "products": [ 
      [ 
       2, 
       10 
      ] 
     ], 
     "delivery_price": 18.34, 

     "product_details": [ 
      { 
       "id": 2, 
       "name": "Ürün 2", 
       "price": "15.76", 
       "created_at": "1491491842", 
       "updated_at": "1491491842", 
       "pivot": { 
        "order_id": 47, 
        "product_id": 2 
       } 
      } 
     ] 
    } 
] 
+1

该文件说什么? –

+0

请阅读[问] - 留下想要的并开始尝试。 – Filburt

+0

对我的问题感到抱歉。这个问题是第一个。我没有编辑我的问题,但我会学习。最后我想把jsonarray解析为我的数组列表 –

回答

0

希望下面是JSON响应你得到

[ { "id": 45, "products": [ [ 1, 10 ], [ 2, 5 ], [ 3, 1 ] ], "delivery_price": 18.86, "product_details": [ { "id": 1, "name": "Ürün 1", "price": "4.56", "created_at": "1491491836", "updated_at": "1491491836", "pivot": { "order_id": 45, "product_id": 1 } }, { "id": 2, "name": "Ürün 2", "price": "15.76", "created_at": "1491491842", "updated_at": "1491491842", "pivot": { "order_id": 45, "product_id": 2 } }, { "id": 3, "name": "Ürün 3", "price": "11.28", "created_at": "1491491849", "updated_at": "1491491849", "pivot": { "order_id": 45, "product_id": 3 } } ] }, { "id": 47, "products": [ [ 2, 10 ] ], "delivery_price": 18.34, "product_details": [ { "id": 2, "name": "Ürün 2", "price": "15.76", "created_at": "1491491842", "updated_at": "1491491842", "pivot": { "order_id": 47, "product_id": 2 } } ] } ]

现在使用org.json.JSONArray,你可以简单的解析此JSON字符串以下

JSONArray jsonArray = new JSONArray(inputJsonString);

和阵列与jsonArray.length()长度。用这个输入,你会得到长度= 2。