如何獲取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
}
}
]
}
]
該文件說什麼? –
請閱讀[問] - 留下想要的並開始嘗試。 – Filburt
對我的問題感到抱歉。這個問題是第一個。我沒有編輯我的問題,但我會學習。最後我想把jsonarray解析爲我的數組列表 –