0

I'm getting an error stating "org.json.JSONException: No value for content" however I've checked the JSON response I'm getting and it contains the value content:

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

I'm not sure exactly what I've done wrong - I must have formatted something incorrectly but I'm not sure what it might be.

SOURCE:

    @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 Answers 3

1

You do not have the key content for every json object. so instead of getString("content"),

use optString("content","defaultValue")

In this case, if there is no value for content, then you return a default value

Sign up to request clarification or add additional context in comments.

1 Comment

String content = jsonObject.optString("content","defaultValue"); results in: org.json.JSONException: No value for content
0

This is the structure of your json :- enter image description here

I am parsing your url and i do this :-

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

Now, as you can see from the above image, object feed has an array named entry Now, json2 is not returning the array entry as seen in the image below, and i cannot hardcode your json in assets folder and parse it because it is encoded in SP1252 encoding and eclipse is showing an error. enter image description here

Your json is also dynamic, so it is sometimes coming valid and sometimes not.

Comments

0

You cannot access the JSONObject "content" because it is a nested object in the JSON string you provided.

From what I see you would first have to get the JSONObject "feed" then the JSONArray "entry" and only then you can invoke .getJSONObject("content") on one of the objects in the array.

EDIT

To get to the name it gets a little tricky, because the name seems to be even further down in the object.

Once you have the JSONArray "entry" try the following:

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));
}

I do have to say it's a bit of a hassle, but this should work, let me know if you need more help.

If you try to parse JSON Objects always pay attention to the format:

  • If something starts with a { its another JSONObject
  • If something starts with a [ it's a JSONArray
  • If something starts with " then you are finally at the string and can invoke .getString() on the identifier.

5 Comments

I tried using JSONArray jsonArray = json.getJSONObject("feed").getJSONArray("entry"); List<Comments> comments = new ArrayList<Comments>(); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); String name = jsonObject.getString("name"); and ended up getting org.json.JSONException: No value for name
@user3009687 I'll add the solution to my answer above.
I also added how to get the content and published string.
Strange... I just used exactly what you posted and it isn't populating the textview: pastebin.com/r6DydbRH
@user3009687 Try again now, I made a minor mistake in the code, it's fixed now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.