1

I am obtaining a JSON response from an API which contains color information.

This is the response:

enter image description here

I want to be able to access the html_code value from the background_colors array within the info JSON object.

Firstly, have tried doing this simply with this code:

result = stack.getBody().getObject().toString(2);
           JSONObject parentObject = new JSONObject(_result);
           JSONArray jr = parentObject.getJSONArray("results");
           JSONObject jb1 = jr.getJSONObject(0);
           System.out.print(jb1);

This prints me out the info Object as expected.

However, if I try and access the JSON Array "background_colors" using this,

JSONObject parentObject = new JSONObject(_result);
            JSONArray jr = parentObject.getJSONArray("results");
            JSONObject jb1 = jr.getJSONObject(0);
            System.out.print(jb1);
            JSONArray jsonArray = 
            jb1.getJSONArray("background_colors");
            System.out.print(jsonArray);

I get this error: No value for "background_colors".

I know this error means that the background_colors array does not exist in the JSONObject but I have no idea how and why this would be the case?

Any help would be much appreciated.

1
  • 1
    background_colors is a property of the info object not the root object from the array. Commented Apr 26, 2017 at 8:24

2 Answers 2

1

background_colors is a property of the info object not the root object from the array.

Try this:

JSONObject parentObject = new JSONObject(_result);
JSONArray jr = parentObject.getJSONArray("results");
JSONObject jb1 = jr.getJSONObject(0).getJSONObject("info");
JSONArray jsonArray = jb1.getJSONArray("background_colors");
System.out.print(jsonArray);
Sign up to request clarification or add additional context in comments.

Comments

0
JSONObject jb1 = jr.getJSONObject(0).getJSONObject("info");
JSONArray jsonArray = jb1.getJSONArray("background_colors");

Comments

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.