我使用Volley JSON请求从Web服务解析JSON。
这就是它的样子:
{
"results": [
{
"category": "Wallet",
"thumbnail": {
"image50": "http://ked.ai/uploads/item/1718946498/60284_1389592558.4e549bae0384ffce63035a7689d824c4_50.jpg",
"image100": "http://ked.ai/uploads/item/1718946498/60284_1389592558.4e549bae0384ffce63035a7689d824c4_100.jpg"
},
"price": "166",
"shop_vanity": "EProductsSibu",
"description": "DESCRIPTION: Good Leather. Made in India.\r\n\r\nPACKAGE CONTENT: Good Condition\r\n\r\nREASON FOR SALE: One more extra\r\n\r\nCOD LOCATION: Sibu, Sarawak",
"item_id": "18319",
"name": "Always Wild Men's Wallet With Chain Ring",
"created_at": "2014-01-13 13:56:01",
"category_friendly": "wallet",
"user_id": "60284"
},
{
"category": "Accessories",
"thumbnail": {
"image50": "http://ked.ai/uploads/item/656783513/1802_1389589673.a4bc91735db236661b207534e72849bb_50.jpg",
"image100": "http://ked.ai/uploads/item/656783513/1802_1389589673.a4bc91735db236661b207534e72849bb_100.jpg"
},
"price": "10",
"shop_vanity": "mhs",
"description": "BROOCH CINCIN SESUAI DIPAKAI OLEH MEREKA YG GEMAR BERTUDUNG BAWAL. BOLEH MENJIMATKAN MASA SEKALIGUS TAMPAK ANGGUN ;)\r\n\r\n",
"item_id": "18307",
"name": "Brooch Cincin",
"created_at": "2014-01-13 13:10:19",
"category_friendly": "accessories",
"user_id": "1802"
},
{
"category": "Accessories",
"thumbnail": {
"image50": "http://ked.ai/uploads/item/656783513/1802_1389589678.8c60ab80a3eea04dc74605d3e4df2d8b_50.jpg",
"image100": "http://ked.ai/uploads/item/656783513/1802_1389589678.8c60ab80a3eea04dc74605d3e4df2d8b_100.jpg"
},
"price": "10",
"shop_vanity": "mhs",
"description": "BROOCH CINCIN SESUAI DIPAKAI OLEH MEREKA YG GEMAR BERTUDUNG BAWAL. BOLEH MENJIMATKAN MASA SEKALIGUS TAMPAK ANGGUN ;)",
"item_id": "18308",
"name": "Brooch Cincin",
"created_at": "2014-01-13 13:10:19",
"category_friendly": "accessories",
"user_id": "1802"
},
{
"category": "Accessories",
"thumbnail": {
"image50": "http://ked.ai/uploads/item/656783513/1802_1389589683.1b19cc2dedfde693784e8cd915b45227_50.jpg",
"image100": "http://ked.ai/uploads/item/656783513/1802_1389589683.1b19cc2dedfde693784e8cd915b45227_100.jpg"
},
"price": "10",
"shop_vanity": "mhs",
"description": "BROOCH CINCIN SESUAI DIPAKAI OLEH MEREKA YG GEMAR BERTUDUNG BAWAL. BOLEH MENJIMATKAN MASA SEKALIGUS TAMPAK ANGGUN ;)",
"item_id": "18309",
"name": "Brooch Cincin",
"created_at": "2014-01-13 13:10:19",
"category_friendly": "accessories",
"user_id": "1802"
},
{
"category": "Accessories",
"thumbnail": {
"image50": "http://ked.ai/uploads/item/656783513/1802_1389589687.93389c64d7e36d7ebfaf58dbbb77914a_50.jpg",
"image100": "http://ked.ai/uploads/item/656783513/1802_1389589687.93389c64d7e36d7ebfaf58dbbb77914a_100.jpg"
},
"price": "10",
"shop_vanity": "mhs",
"description": "BROOCH CINCIN SESUAI DIPAKAI OLEH MEREKA YG GEMAR BERTUDUNG BAWAL. BOLEH MENJIMATKAN MASA SEKALIGUS TAMPAK ANGGUN ;)",
"item_id": "18310",
"name": "Brooch Cincin",
"created_at": "2014-01-13 13:10:19",
"category_friendly": "accessories",
"user_id": "1802"
},
{
"category": "Accessories",
"thumbnail": {
"image50": "http://ked.ai/uploads/item/656783513/1802_1389589692.21d560bc5aec0e594bfc1598b9c8f3ca_50.jpg",
"image100": "http://ked.ai/uploads/item/656783513/1802_1389589692.21d560bc5aec0e594bfc1598b9c8f3ca_100.jpg"
},
"price": "10",
"shop_vanity": "mhs",
"description": "BROOCH CINCIN SESUAI DIPAKAI OLEH MEREKA YG GEMAR BERTUDUNG BAWAL. BOLEH MENJIMATKAN MASA SEKALIGUS TAMPAK ANGGUN ;)",
"item_id": "18311",
"name": "Brooch Cincin",
"created_at": "2014-01-13 13:10:19",
"category_friendly": "accessories",
"user_id": "1802"
},
{
"category": "Accessories",
"thumbnail": {
"image50": "http://ked.ai/uploads/item/656783513/1802_1389589697.01e964c85afb4a2b1e8caf2bcf5ad169_50.jpg",
"image100": "http://ked.ai/uploads/item/656783513/1802_1389589697.01e964c85afb4a2b1e8caf2bcf5ad169_100.jpg"
},
"price": "10",
"shop_vanity": "mhs",
"description": "BROOCH CINCIN SESUAI DIPAKAI OLEH MEREKA YG GEMAR BERTUDUNG BAWAL. BOLEH MENJIMATKAN MASA SEKALIGUS TAMPAK ANGGUN ;)",
"item_id": "18312",
"name": "Brooch Cincin",
"created_at": "2014-01-13 13:10:19",
"category_friendly": "accessories",
"user_id": "1802"
}
]
}
但是,我得到了一个不完整的JSON响应。仅成功解析了前两个结果对象。这是解析json的方法:
private void loadItemList() {
mRequestQueue = Volley.newRequestQueue(getActivity());
pd = ProgressDialog.show(getActivity(), null, "Loading...");
String url = Constants.LATEST_ITEM_LIST;
JsonObjectRequest jr = new JsonObjectRequest(Request.Method.GET, url,
null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.i(TAG, response.toString());
parseJSON(response);
va.notifyDataSetChanged();
pd.dismiss();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
pd.dismiss();
Log.i(TAG, error.getMessage());
errorMsg = VolleyErrorHelper.getMessage(error,
getActivity());
Toast.makeText(getActivity(), errorMsg,
Toast.LENGTH_LONG).show();
}
});
mRequestQueue.add(jr);
}
private void parseJSON(JSONObject json) {
if (json != null) {
try {
JSONArray items = json.getJSONArray("results");
for (int i = 0; i < json.length(); i++) {
JSONObject itemlist = items.getJSONObject(i);
ItemListModel ilm = new ItemListModel();
ilm.setCategory(itemlist.optString(Constants.TAG_CATEGORY_NAME));
ilm.setItem_id(itemlist.optString(Constants.TAG_ITEM_ID));
ilm.setName(itemlist.optString(Constants.TAG_ITEM_NAME));
ilm.setPrice(itemlist.optString(Constants.TAG_ITEM_PRICE));
ilm.setUser_id(itemlist.optString(Constants.TAG_USER_ID));
JSONObject thumb = itemlist.getJSONObject(Constants.TAG_ITEM_THUMBNAIL);
ilm.setThumbnail(thumb.optString(Constants.TAG_ITEM_THUMB));
arrItemList.add(ilm);
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
Log.d("Item list: ", "null");
}
}
我的逻辑上面有什么不对吗?
答案 0 :(得分:1)
在这里你已经解析了你的json数组
JSONArray items = json.getJSONArray("results");
因此,在for循环中而不是使用 json 对象时,您必须使用 项目 JsonArray。因此,您可以访问其中包含的其他数据。
从
改变 for (int i = 0; i < json.length(); i++)
到
for (int i = 0; i < items .length(); i++)
答案 1 :(得分:0)
试试这个..
因为你的for循环从 JSONObject 得到json.length();
的长度,你需要从items.length();
JSONArray 得到长度而不是的JSONObject 强>
JSONArray items = json.getJSONArray("results");
for (int i = 0; i < items.length(); i++) {
答案 2 :(得分:0)
在For循环中,您已经定义了json
对象的数组大小,而不是JsonArray
,这可能就是为什么它没有给出正确的结果。
将items
JsonArray的大小与items.length()
中的for循环一样。
试试如下:
JSONArray items = json.getJSONArray("results");
for (int i = 0; i < items.length(); i++) {