[英]JSON parsing for Expandable ListView in Android
我是Android的新手,我想在Android中制作Expandable Listview ,但是在解析json时遇到问题。 我想解析以下json:
[
{
"ref": "000051",
"name": "DURA GREEN",
"desc": "DURA GREEN 30 TO 0",
"price": "29.000",
"skus": [
{
"barcode": "000051046008",
"color": "KHAKI R",
"size": "42",
"price": "29.00"
},
{
"barcode": "000051046009",
"color": "KHAK",
"size": "44",
"price": "29.00"
}
]
},
{
"ref": "000072",
"name": "WHITE DURA",
"desc": "WHITE DURABLE 30 - 54",
"price": "29.000",
"skus": [
{
"barcode": "000072043010",
"color": "WHITE L",
"size": "48",
"price": "29.00"
},
{
"barcode": "000072043011",
"color": "WHITE L",
"size": "50",
"price": "29.00"
}
]
},
{
"ref": "000078",
"name": "EBAY KARLSON",
"desc": "SMART WHITE TROUSERS WITH PRIE",
"price": "34.000",
"skus": [
{
"barcode": "000078042002",
"color": "WHITE R",
"size": "30",
"price": "34.00"
}
]
},
{
"ref": "000053",
"name": "DURA BLACK",
"desc": "DURA BLACK WAIST 30 TO 54",
"price": "19.000",
"skus": [
{
"barcode": "000053038004",
"color": "BLACK S",
"size": "36",
"price": "19.00"
}
]
},
{
"ref": "000080",
"name": "DURA WHITE EBY",
"desc": "WHITE DURABLE 30 - 44",
"price": "34.000",
"skus": [
{
"barcode": "000080042007",
"color": "WHITE R",
"size": "51",
"price": "37.00"
}
]
},
{
"ref": "000025",
"name": "DURABLEPRESS",
"desc": "DURABLEPRESS 30 TO 50 WAIST",
"price": "19.000",
"skus": [
{
"barcode": "000025022002",
"color": "NAVY S",
"size": "30",
"price": "19.00"
},
{
"barcode": "000025022003",
"color": "NAVY S",
"size": "30",
"price": "19.00"
}
]
}
]
LogCat将错误显示为:
08-03 23:38:34.664:W / System.err(23554):org.json.JSONException:索引2超出范围[0..2)08-03 23:38:34.674:W / System.err( 23554):位于org.json.JSONArray.get(JSONArray.java:282)08-03 23:38:34.674:W / System.err(23554):位于org.json.JSONArray.getJSONObject(JSONArray.java:510 )08-03 23:38:34.674:W / System.err(23554):位于com.example.webservice.NetworkOperation.SearchList(NetworkOperation.java:654)08-03 23:38:34.674:W / System.err (23554):位于com.example.webservice.NetworkOperation.doInBackground(NetworkOperation.java:98)08-03 23:38:34.674:W / System.err(23554):位于com.example.webservice.NetworkOperation.doInBackground( NetworkOperation.java:1)08-03 23:38:34.674:W / System.err(23554):at android.os.AsyncTask $ 2.call(AsyncTask.java:288)08-03 23:38:34.674:W /System.err(23554):在java.util.concurrent.FutureTask.run(FutureTask.java:237)08-03 23:38:34.674:W / System.err(23554):在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231)08-03 23:38:34.674:W / System.err(23554):at java.util .concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)08-03 23:38:34.674:W / System.err(23554):at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:587) 08-03 23:38:34.674:W / System.err(23554):at java.lang.Thread.run(Thread.java:841)
使用的代码是:
try {
group_list = new ArrayList<Model_Search_Group>();
ArrayList<Model_Search_Child> child_list;
ResponseHandler<String> responseHandler = new BasicResponseHandler();
result = httpClient.execute(postRequest, responseHandler);
System.out.println("---result" + result);
JSONArray jsonarray = new JSONArray(result);
for (int i = 0; i < jsonarray.length(); i++) {
child_list = new ArrayList<Model_Search_Child>();
JSONObject jsonObj = jsonarray.getJSONObject(i);
System.out.println("---json" + jsonObj);
Model_Search_Group data = new Model_Search_Group();
data.setItemRef((jsonObj.getString("ref")));
data.setName((jsonObj.getString("name")));
data.setDesc((jsonObj.getString("desc")));
data.setPrice((jsonObj.getString("price")));
JSONArray obj2 = jsonObj.getJSONArray("skus");
for (int j = 0; j < obj2.length(); j++) {
JSONObject json = obj2.getJSONObject(i);
System.out.println("---json" + json);
Model_Search_Child data2 = new Model_Search_Child();
data2.setBarcode((json.getString("barcode")));
data2.setColor((json.getString("color")));
data2.setSize((json.getString("size")));
data2.setPrice(json.getString("price"));
child_list.add(data2);
}
data.setChildItems(child_list);
group_list.add(data);
System.out.println("groupsize" + group_list.size());
}
在第二个for循环中,替换JSONObject json = obj2.getJSONObject(i);
通过JSONObject json = obj2.getJSONObject(j);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.