Home Android JsonObject Error org.json.JSONException: No value
Reply: 3

Android JsonObject Error org.json.JSONException: No value

William Randall
1#
William Randall Published in 2018-01-12 22:25:33Z

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();
            }
        }
    });
}
Akhil
2#
Akhil Reply to 2018-01-13 03:20:11Z

You are reading values from wrong object. Try this

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

Instead of

                JSONObject value = ((JSONObject) json.get(key));
gorp88
3#
gorp88 Reply to 2018-01-15 17:16:22Z
"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.

Mehdi Yari
4#
Mehdi Yari Reply to 2018-01-13 06:24:24Z
JSONObject jsonObjectRoot = new JSONObject("Your_Json");
JSONObject jsonObjectData = jsonObjectRoot.getJSONObject("data");
JSONObject jsonObjectAtrox = jsonObjectData.getJSONObject("Aatrox");
JSONObject jsonObjectAtroxInfo = jsonObjectAtrox.getJSONObject("info");

And so on ...

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.316525 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO