Search code examples
javaandroidarraysjsonjsonexception

Getting no value from JSON array in Android


I have a weird problem and I hope that you can help me with it..

The problem is as follows: I have created a javacode to resolve a query from JSON but I can't parse it to a String... I also tried to get the String 0, but that also does not work.

Underneath, the code from Java:

JSONArray json_data = new JSONArray(result);
           for (int i = 0; i < json_data.length(); i++) {
                code = json_data.getJSONObject(i).getInt("code");
                pictureID = json_data.getJSONObject(i).getString(
                        "Picture_ID");
                System.out.println(code);
                System.out.println(pictureID);
            }

This is the response I get from JSON:

[{"code":1}][{"Picture_ID":"74","0":"74"}]

But this is the error I get from the catch clause:

org.json.JSONException: No value for Picture_ID

Again, Thank you very much for helping out!


Solution

  • Once you check key value "Picture_ID" is correct, if it correct try with this code...

    for (int i = 0; i < innerProjectarray.length(); i++) {
    
    JSONObject obj = innerProjectarray.getJSONObject(i);
    
    String projNum = obj.optString("code"); 
    String projName = obj.optString("Picture_ID"); 
    // and use both values. 
    }