【问题标题】:Getting JSONExeption: No Value获取 JSONException:无价值
【发布时间】:2013-08-30 12:49:27
【问题描述】:

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

有什么问题?为什么效果不好?

【问题讨论】:

  • What's the problem?: 你不了解你的响应数据结构...jObj没有“title”属性,它只有“object”属性,它是一个数组...和-1因为这个问题是题外话,因为Questions asking for code must demonstrate a minimal understanding of the problem being solved.

标签: android json getjson jsonexception


【解决方案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");


    }

【讨论】:

    【解决方案2】:

    您需要先获取您的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);
    }
    

    【讨论】:

    • @HaykNahapetyan,我的错。尝试将“Object”设为小写(参见固定代码)。
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2013-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多