IcyRelic
IcyRelic

Reputation: 167

Android JsonObject Error org.json.JSONException: No value

When reading a JSON string in java for Android it is giving the error No value found when there is a value to find. The json string comes from (http://ddragon.leagueoflegends.com/cdn/7.24.1/data/en_US/champion.json) I need help figuring out where I went wrong reading the JSON string.

The error I am getting is below.

W/System.err: org.json.JSONException: No value for Aatrox
W/System.err:     at org.json.JSONObject.get(JSONObject.java:389)
W/System.err:     at com.icyrelic.lolsummoner.api.LeagueAPI$3.onSuccess(LeagueAPI.java:146)
W/System.err:     at com.icyrelic.lolsummoner.api.LeagueAPI$1.onResponse(LeagueAPI.java:51)
W/System.err:     at com.icyrelic.lolsummoner.api.LeagueAPI$1.onResponse(LeagueAPI.java:41)
W/System.err:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:78)
W/System.err:     at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
W/System.err:     at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:106)
W/System.err:     at android.os.Handler.handleCallback(Handler.java:751)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
W/System.err:     at android.os.Looper.loop(Looper.java:154)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6776)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

Code from LeagueAPI.java

public static void createRequest(String url, final VolleyCallback callback) {



    final StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {

                        Object json = new JSONTokener(response).nextValue();


                        if(json instanceof JSONObject) {
                            callback.onSuccess(new JSONObject(response));
                        } else if(json instanceof  JSONArray) {
                            callback.onSuccess(new JSONArray(response));
                        } else {
                            throw new Exception("Unknown JSON");
                        }



                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    });
    queue.add(stringRequest);
}

private void setupChampionData() {
    createRequest("http://ddragon.leagueoflegends.com/cdn/7.24.1/data/en_US/champion.json", new VolleyCallback() {
        @Override
        public void onSuccess(Object response) {
            JSONObject json = ((JSONObject) response);
            try {
                JSONObject champions = json.getJSONObject("data");


                Iterator<String> temp = champions.keys();
                System.out.println(json.toString());
                while (temp.hasNext()) {
                    String key = temp.next();

                    JSONObject value = ((JSONObject) json.get(key));

                    championData.put(value.getInt("key"), value);

                }




            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

Upvotes: 0

Views: 5355

Answers (3)

gorp88
gorp88

Reputation: 105

"type":"champion",
"format":"standAloneComplex",
"version":"7.24.1",
"data":{  
"Aatrox":{  },
"Ahri":{  },
}..     

if you see the json response "Aatrox" is JSONObject inside the data object. Modify the object reader. You may get it.

Upvotes: 0

user7085096
user7085096

Reputation:

JSONObject jsonObjectRoot = new JSONObject("Your_Json");
JSONObject jsonObjectData = jsonObjectRoot.getJSONObject("data");
JSONObject jsonObjectAtrox = jsonObjectData.getJSONObject("Aatrox");
JSONObject jsonObjectAtroxInfo = jsonObjectAtrox.getJSONObject("info");

And so on ...

Upvotes: 1

Akhil
Akhil

Reputation: 6697

You are reading values from wrong object. Try this

                JSONObject value = ((JSONObject) champions.get(key));

Instead of

                JSONObject value = ((JSONObject) json.get(key));

Upvotes: 1

Related Questions