-1

Here's the JSON response.

The following works:

jsonObject.get("title").getAsString();
jsonObject.get("author").getAsJsonObject().get("profile_photo").getAsString();

but jsonObject.get("primary_photo").getAsString() (from line 60) returns an Unsupported Exception: null error. I've tried replacing getAsString() with toString() but the latter returns an empty string.

3
  • 1
    Which JsonObject class you use in the code? Commented Sep 29, 2017 at 7:30
  • 1
    Have a look at this question i have had the same issue. Commented Sep 29, 2017 at 7:34
  • @Elka I imported this com.google.gson.JsonObject;. Commented Sep 29, 2017 at 7:44

4 Answers 4

1

Try this

Use optJSONObject and optString in your code . And the root is [],so you should use JSONArray

try {
    JSONArray jsonArray = new JSONArray(response);
    for (int i = 0; i < jsonArray.length(); i++) {
         //  use optJSONObject
         JSONObject author = jsonArray.optJSONObject(i).optJSONObject("author");
         // use optString , it did not return null
         String profile_photo = author.optString("profile_photo");
         String primary_photo = jsonArray.optJSONObject(i).optString("primary_photo");
    }
} catch (JSONException e) {
        e.printStackTrace();
}

Gson

JsonArray jsonElements = new JsonParser().parse(response).getAsJsonArray();
for (int i = 0; i < jsonElements.size(); i++) {
        JsonObject jObject = jsonElements.get(i).getAsJsonObject();
        // edited here 
        String primary_photo = jObject.get("primary_photo").getAsString();
        JsonObject author = jObject.getAsJsonObject("author");
        String profile_photo = author.get("profile_photo").getAsString();
}
Sign up to request clarification or add additional context in comments.

4 Comments

I don't have any issues with the author and its nested object profile_photo. My issue is with the primary_photo key (line 60).
I edited it .@Bargain23
shouldn't this author.optString("author") be author.optString("primary_photo")?
It should be String primary_photo = jsonArray.optJSONObject(i).optString("primary_photo"); in your code .
1

I think the Problem is, that your JSON response contains an Array. You have to create an JSONArray from the response-string and than iterate about it to get your values. Or if this array always contains one object:

JSONArray jsonArray = new JSONArray(response);
JSONObject jsonObject = jsonArray.getJSONObject(0);

2 Comments

The get method is performed inside a for loop.
Why you use Gson ? Android comes with an own library: org.json.JSONObject
1
Try this,

try {
    JSONArray jsonArray = new JSONArray(response);
    for (int i = 0; i < jsonArray.length(); i++) {

         JSONObject author = jsonArray.get(i).getJSONObject"author");

         String profile_photo = author.getString("author");
    }
} catch (JSONException e) {
        e.printStackTrace();
}

Comments

1

Try this:

try {
            JSONArray jarr=new JSONArray(response);   // response is whole json response
            for (int i=0;i<jarr.length();i++){
                JSONObject jobj=jarr.getJSONObject(i);   
                String primary_photo=jobj.getString("primary_photo");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.