获取JSONExeption:无数值

hrirmatl  于 2023-03-24  发布在  其他
关注(0)|答案(3)|浏览(112)

下面是我的初始化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
有什么问题?为什么不好用?

h6my8fg2

h6my8fg21#

请尝试此代码进行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");

    }
7uzetpgm

7uzetpgm2#

你需要先得到你的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);
}
zbdgwd5y

zbdgwd5y3#

您收到此异常是因为"title"键不在您的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包含所需的字符串。

相关问题