Android代码
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(
"http://192.168.1.104/Ceylon_Steel/service/getCategoriesAndItems");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpClient.execute(httpPost, responseHandler);
JSONObject posts = new JSONObject(responseBody);
JSONArray jArray = posts.getJSONArray("categories");
Log.i("Tag", jArray.toString());
Json
categories_and_items({
categories: [
{
id: "c2",
item: [
{
itemId: "9",
itemCode: "FG039062",
description: "E1-LINER 38x26-R: NAKAYAMA NCF-2B IMPACT CHUSHER",
price: "0.00",
quantity: "100000"
},
]
},
]
})
在这里,我尝试使用上面的代码从json中检索数据并将其打印在日志上但是尝试我得到以下错误。
12-06 10:11:38.578: W/System.err(3695): org.json.JSONException: Value categories_and_items of type java.lang.String cannot be converted to JSONObject
答案 0 :(得分:1)
我认为json无效。
是什么
categories_and_items( ... )
答案 1 :(得分:0)
您的JSON回复无效。它应始终以{...}
括号开头,而不是(..)
括号。
如果可能,请删除(..)
括号,如下所示更改您的回复:
categories_and_items{
categories: [
{
id: "c2",
item: [
{
itemId: "9",
itemCode: "FG039062",
description: "E1-LINER 38x26-R: NAKAYAMA NCF-2B IMPACT CHUSHER",
price: "0.00",
quantity: "100000"
},
]
},
]
}