org.json.JSONException:没有内容的值

时间:2013-12-11 18:02:35

标签: java android json

我收到一条错误,指出“org.json.JSONException:没有内容的值”但是我已经检查了我得到的JSON响应,它包含了值内容:

http://gdata.youtube.com/feeds/api/videos/M41q4blaJ7w/comments?v=2&alt=json&start-index=1&max-results=50&prettyprint=true

我不确定我做错了什么 - 我必须格式化错误,但我不确定它可能是什么。

来源:

    @Override
    protected Void doInBackground(Void... arg0) {
        try {

            HttpClient client = new DefaultHttpClient();

            HttpUriRequest request = new HttpGet(
                    "http://gdata.youtube.com/feeds/api/videos/"
                            + video_id
                            + "/comments?v=2&alt=json&start-index=1&max-results=50&prettyprint=true");

            HttpResponse response = client.execute(request);

            String jsonString = StreamUtils.convertToString(response
                    .getEntity().getContent());

            JSONObject json = new JSONObject(jsonString);
            JSONArray jsonArray = json.getJSONObject("content").getJSONArray(
                    "name");

            List<Comments> comments = new ArrayList<Comments>();

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);

                String name = jsonObject.getString("name");
                String content = jsonObject.getString("content");
                String published = jsonObject.getString("published");

                comments.add(new Comments(name, content, published));
            }

            CommentsLibrary lib = new CommentsLibrary(jsonString, jsonString, jsonString);

            Bundle data = new Bundle();
            data.putSerializable(LIBRARY, lib);

            Message msg = Message.obtain();
            msg.setData(data);
            replyTo.sendMessage(msg);

        } catch (ClientProtocolException e) {
            Log.e("Feck", e);
        } catch (IOException e) {
            Log.e("Feck", e);
        } catch (JSONException e) {
            Log.e("Feck", e);
        }
        return null;
    }

    /*
     * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
     */
    @Override
    protected void onPostExecute(Void result) {
        TextView nameTv = (TextView) findViewById(R.id.name);   
        nameTv.setText(com.idg.omv.domain.CommentsLibrary.getName());

        TextView contentTv = (TextView) findViewById(R.id.content); 
        contentTv.setText(com.idg.omv.domain.CommentsLibrary.getContent());

        TextView publishedTv = (TextView) findViewById(R.id.published); 
        publishedTv.setText(com.idg.omv.domain.CommentsLibrary.getPublished());
    }
}

}

3 个答案:

答案 0 :(得分:0)

您没有每个json对象的关键内容。所以而不是getString("content")

使用 optString("content","defaultValue")

在这种情况下,如果内容没有值,则返回默认值

答案 1 :(得分:0)

您无法访问JSONObject "content",因为它是您提供的JSON字符串中的嵌套对象。

从我看到你首先必须获得JSONObject "feed"然后JSONArray "entry",然后才能在数组中的一个对象上调用.getJSONObject("content")

编辑

要获得名称,它会变得有点棘手,因为该名称似乎在对象中更进一步。

一旦你JSONArray "entry"尝试以下内容:

for (int i = 0; i < jsonArray.length(); i++) { 
    JSONObject entry = jsonArray.getJSONObject(i); 
    JSONObject contentObject = entry.getJSONObject("content");
    String content = contentObject.getString("$t");

    JSONObject publishedObject = entry.getJSONObject("published");
    String published = publishedObject.getString("$t");

    JSONArray authorArray = entry.getJSONArray("author");
    JSONObject authorObject = authorArray.getJSONObject(0);
    JSONObject nameObject = authorObject.getJSONObject("name");
    String name = nameObject.getString("$t");

    comments.add(new Comments(name, content, published));
}

我不得不说这有点麻烦,但这应该有用,如果您需要更多帮助,请告诉我。

如果您尝试解析JSON对象,请始终注意格式:

  • 如果某个内容以{另一个JSONObject
  • 开头
  • 如果某件事以[开头,则为JSONArray
  • 如果某些内容以"开头,那么您最终会遇到该字符串,并且可以在标识符上调用.getString()

答案 2 :(得分:0)

这是你的json的结构: - enter image description here

我正在解析你的网址,我这样做: -

JSONObject json = jParser.getJSONFromUrl(url);
JSONObject json2 = json.getJSONObject("feed");

现在,从上图中可以看出,对象 Feed 有一个名为条目的数组 现在,json2没有返回数组entry,如下图所示,我无法在资源文件夹中硬编码你的json并解析它,因为它是用SP1252编码编码的,eclipse显示错误。 enter image description here

你的json也是动态的,所以它有时会变得有效,有时也不会。

相关问题