简体   繁体   中英

How to access the JSON element?

 @Override
    protected void onPostExecute(String response) {
        String firstName = null;
        String lastName = null;
        try {
            JSONObject jsonResponse = new JSONObject(response);
            JSONArray jsonArray = jsonResponse.getJSONArray("");
            JSONObject userInfo = jsonArray.getJSONObject(0);

            firstName = userInfo.getString("id");
            lastName = userInfo.getString("id");

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

I have this JSON file https://api.myjson.com/bins/1a5t7w How i can acces to thats values, i used this code but it doesn`t work? It returns @null@

  1. You are getting a JSON array in the response.
  2. To parse this array you need to pass the response to JSONArray object like below
try {
    JSONArray rspArr = new JSONArray(response);
    for(int i= 0; i< rspArr.length(); i++){
        JSONObject jsonObject = (JSONObject) rspArr.get(i);
        //Your logic
    }
}catch (Exception e ){
        //log exception
}
// since you know is an array, create a JSONArray based on your response String    
JSONArray array = JSONArray(response)
// obtain the first element of your array as a JSONObject
JSONObject jsonObject = array.getJSONObject(0)
// once you get the jsonObject you can start getting the values you need
String id = jsonObject.getString("id")

Hope it helps!

Answer by Arunangshu seems correct, you need to extract the JSONArray first because the API is returning JSONArray(array of JSON objects) and not one big JSON object.

One can use http://jsonviewer.stack.hu/ for viewing JSON. It gives better understanding of the JSON structure.

For Java, one can use http://www.jsonschema2pojo.org/ for converting JSON object(not array) to Java Pojo class (select the options carefully).

Following is the first object from json array -

{"id":48191,"title":"Apple Crumble Recipe","image":"https://spoonacular.com/recipeImages/48191-312x231.jpg","imageType":"jpg","usedIngredientCount":1,"missedIngredientCount":2,"missedIngredients":[{"id":4073,"amount":35.0,"unit":"g","unitLong":"grams","unitShort":"g","aisle":"Milk, Eggs, Other Dairy","name":"margarine","original":"35 g margarine or butter","originalString":"35 g margarine or butter","originalName":"margarine or butter","metaInformation":[],"meta":[],"image":"https://spoonacular.com/cdn/ingredients_100x100/butter-sliced.jpg"},{"id":8120,"amount":35.0,"unit":"g","unitLong":"grams","unitShort":"g","aisle":"Cereal","name":"rolled oats","original":"35 g rolled oats","originalString":"35 g rolled oats","originalName":"rolled oats","metaInformation":[],"meta":[],"image":"https://spoonacular.com/cdn/ingredients_100x100/rolled-oats.jpg"}],"usedIngredients":[{"id":9003,"amount":400.0,"unit":"g","unitLong":"grams","unitShort":"g","aisle":"Produce","name":"apples","original":"400 g cooking apples peeled cored and quartered","originalString":"400 g cooking apples peeled cored and quartered","originalName":"cooking apples peeled cored and quartered","metaInformation":["cored","peeled","quartered"],"meta":["cored","peeled","quartered"],"image":"https://spoonacular.com/cdn/ingredients_100x100/apple.jpg"}],"unusedIngredients":[],"likes":965}

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