简体   繁体   中英

I can parse JsonObject but having some difficulty with JsonArray using volley library

I want to get the data from the 'number' and 'step' properties, but can't seem to find a solution. I'm a beginner at android, so would really appreciate your help

**Here is the json ** https://api.myjson.com/bins/135pdu

Here is my code

JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, uri,null,
                    new Response.Listener<JSONArray>() {
                        @Override
                        public void onResponse(JSONArray response) {
                            String count,step;
                            try {
                                Log.d("VolleyResponse","Response:" + response);
                                for(int i = 0; i<response.length();i++){

                                    JSONObject current = response.getJSONObject(i);
                                    JSONArray arrayStatus = current.getJSONArray("steps");
                                    for(int a = 0; a<arrayStatus.length(); a++){
                                        JSONObject recipe_step = arrayStatus.getJSONObject(i);
                                        count = recipe_step.getString("number");
                                        step = recipe_step.getString("step");

                                        steps.append("Step " +count+":"+step+"\n\n");
                                    }
                                }
                            }catch(JSONException e){
                                e.printStackTrace();
                            }
                        }

                    }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("VolleyError", error.toString());
                }

count = recipe_step.getString("number");

步骤号是整数而不是字符串,请尝试:

int count; count = recipe_step.getInt("number");

  JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, uri,null,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        String count,step;

                        try {
                            Log.d("VolleyResponse","Response:" + response);
                            for(int i = 0; i<response.length();i++){

                                JSONObject current = response.getJSONObject(i);
                                JSONArray arrayStatus = current.getJSONArray("steps");
                                for(int a = 0; a<arrayStatus.length(); a++){
                                    JSONObject recipe_step = arrayStatus.getJSONObject(i);
                                    count = recipe_step.getInt("number")+"";
                                    step = recipe_step.getString("step");

                                    steps.append("Step " +count+":"+step+"\n\n");
                                }
                            }
                        }catch(JSONException e){
                            e.printStackTrace();
                        }
                    }

                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("VolleyError", error.toString());
            }

use getInt() for numbers

I believe in smart work instead of long work.

I prefer 2 line code to parse instead of writing whole things to parse json.

Type listType = new TypeToken<List<Response>>() {}.getType();
List<Response> yourList = new Gson().fromJson(yourJson, listType);

Now all values are in your list, use it anywhere :)

Google Gson is a well known library to parse json. Just add a small library implementation 'com.google.code.gson:gson:2.8.4' to your app level build.gradle.

Thats all, Just you have to make pojo (or model) classes that will hold variables of your response.

-----------------------------------com.example.Equipment.java-----------------------------------

package com.example;


public class Equipment {

private Integer id;
private String name;
private String image;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getImage() {
return image;
}

public void setImage(String image) {
this.image = image;
}

}

-----------------------------------com.example.Ingredient.java-----------------------------------

package com.example;


public class Ingredient {

private Integer id;
private String name;
private String image;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getImage() {
return image;
}

public void setImage(String image) {
this.image = image;
}

}

-----------------------------------com.example.Response.java-----------------------------------

package com.example;

import java.util.List;

public class Response {

private String name;
private List<Step> steps = null;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public List<Step> getSteps() {
return steps;
}

public void setSteps(List<Step> steps) {
this.steps = steps;
}

}

-----------------------------------com.example.Step.java-----------------------------------

package com.example;

import java.util.List;

public class Step {

private Integer number;
private String step;
private List<Ingredient> ingredients = null;
private List<Equipment> equipment = null;

public Integer getNumber() {
return number;
}

public void setNumber(Integer number) {
this.number = number;
}

public String getStep() {
return step;
}

public void setStep(String step) {
this.step = step;
}

public List<Ingredient> getIngredients() {
return ingredients;
}

public void setIngredients(List<Ingredient> ingredients) {
this.ingredients = ingredients;
}

public List<Equipment> getEquipment() {
return equipment;
}

public void setEquipment(List<Equipment> equipment) {
this.equipment = equipment;
}

}

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