-2

这是我初始化 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

有什么问题?为什么它不能正常工作?

4

3 回答 3

1

请尝试使用此代码进行 json 解析

 JsonArray object= json.getJSONArray("object");

    // looping through All Object
    for(int i = 0; i < object.length(); i++){
        JSONObject c = object.getJSONObject(i);

        // Storing each json item in variable
        String earning = c.getString("earnings");


    }
于 2013-08-30T12:56:11.660 回答
1

你需要先得到你的JSONArray对象:

JSONArray array = jObj.getJSONArray("object");
for (int i = 0; i < array.length(); i++) {
    JSONObject json = array.getJSONObject(i);
    String str = jObj.getString("title");
    //Doubt you want to show alert for each one. 
}

或者,您可以使用droidQuery ($) 来简化解析:

Object[] array = $.makeArray(jObj.getJSONArray("object"));
for (Object obj : array) {
    JSONObject json = (JSONObject) obj;
    Map<String, ?> map = $.map(json);
    String str = map.get("title");
    //maybe show toast instead:
    $.with(this).toast(title, Toast.LENGTH_SHORT);
}
于 2013-08-30T12:58:51.167 回答
0

你得到这个异常是因为"title"key 不直接在你的jObj.

解析如下,

String data = EntityUtils.toString(entity);
                JSONObject jObj=new JSONObject(data);

                JSONArray jarray=jObj.getJSONArray("object");
                for(int i=0;i<jarray.length();i++)
                {
                    JSONObject obj=jarray.getJSONObject(i);
                    String title=obj.getString("title");
                }

title包含您需要的字符串。

于 2013-08-30T12:59:07.783 回答