下面是我的初始化Json对象的函数
public void setJsonsObject() throws ClientProtocolException, IOException, JSONException {
StringBuilder url = new StringBuilder(URL);
HttpGet get = new HttpGet(url.toString());
HttpResponse response = client.execute(get);
int status = response.getStatusLine().getStatusCode();
if(status == 200) {
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
jObj = new JSONObject(data);
//JSONObject first = jsons.getJSONObject(1);
} else {
Toast.makeText(MainActivity.this, "error", Toast.LENGTH_SHORT);
}
}
在此之后我的jObj
{
"object": [{
"earnings": "0",
"sizes": "",
"original_price": "100",
"add_date": "1376597956",
"photo_two": "http:\/\/yoors.com\/api\/files\/520d3480dfafbyoors.jpg",
"id": "1",
"photo_three": "",
"category": "Accessories",
"title": "Minions",
"listing_price": "80",
"description": "Cute minions for sale",
"users_user_id": "3",
"update_date": "1376597956",
"condition_p": "Used ",
"photo_one": "http:\/\/yoors.com\/api\/files\/520d37c409992yoors.jpg"
}, {
"earnings": "0",
"sizes": "4",
"original_price": "20",
"add_date": "1376681417",
"photo_two": "",
"id": "2",
"photo_three": "",
"category": "Jeans",
"title": "cute shorts",
"listing_price": "20",
"description": "shorts you'll love them",
"users_user_id": "4",
"update_date": "1376681417",
"condition_p": "Brand",
"photo_one": "http:\/\/yoors.com\/api\/files\/520e7dc94728cyoors.jpg"
}, {
"earnings": "0",
"sizes": "xl",
"original_price": "25",
"add_date": "1376683240",
"photo_two": "http:\/\/yoors.com\/api\/files\/520e84e8aff0fyoors.jpg",
"id": "3",
"photo_three": "http:\/\/yoors.com\/api\/files\/520e84e8b06dfyoors.jpg",
"category": "Accessories",
"title": "very cute handbag",
"listing_price": "25",
"description": "nice clothes",
"users_user_id": "4",
"update_date": "1376683240",
"condition_p": "Brand",
"photo_one": "http:\/\/yoors.com\/api\/files\/520e84e8af73dyoors.jpg"
}, {
"earnings": "0",
"sizes": "size fits all",
"original_price": "30",
"add_date": "1377025807",
"photo_two": "",
"id": "4",
"photo_three": "",
"category": "Accessories",
"title": "bling girl watch",
"listing_price": "30",
"description": "this watch is made in los angeles",
"users_user_id": "4",
"update_date": "1377025807",
"condition_p": "Brand",
"photo_one": "http:\/\/yoors.com\/api\/files\/5213bf0f403d8yoors.jpg"
}, {
"earnings": "0",
"sizes": "size 2-7",
"original_price": "20",
"add_date": "1377026129",
"photo_two": "",
"id": "5",
"photo_three": "",
"category": "Jeans",
"title": "cute jean shorts",
"listing_price": "20",
"description": "shorts with pockets hanging out",
"users_user_id": "4",
"update_date": "1377026129",
"condition_p": "Brand",
"photo_one": "http:\/\/yoors.com\/api\/files\/5213c051f03d3yoors.jpg"
}, {
"earnings": "0",
"sizes": "all sizes",
"original_price": "30",
"add_date": "1377026853",
"photo_two": "",
"id": "6",
"photo_three": "",
"category": "Dress",
"title": "very cute outfit",
"listing_price": "30",
"description": "this outfit is one of a kind",
"users_user_id": "4",
"update_date": "1377026853",
"condition_p": "Brand",
"photo_one": "http:\/\/yoors.com\/api\/files\/5213c325108efyoors.jpg"
}]}
之后,我尝试从jsonObject
获取一些信息
setJsonsObject();
String str = jObj.getString("title");
AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Title");
alertDialog.setMessage(str);
alertDialog.show();
它给出了JSONExeption: No value for title
有什么问题?为什么不好用?
3条答案
按热度按时间h6my8fg21#
请尝试此代码进行JSON解析
7uzetpgm2#
你需要先得到你的
JSONArray
对象:或者,您可以使用我的droidQuery库($)来简化解析:
zbdgwd5y3#
您收到此异常是因为
"title"
键不在您的jObj
中。如下所述进行解析,
title
包含所需的字符串。