简体   繁体   中英

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();
            }
        }
    });
}

You are reading values from wrong object. Try this

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

Instead of

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

And so on ...

"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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM