我可以解析JsonObject但使用齐射库在JsonArray上遇到一些困难

时间:2018-05-18 18:48:39

标签: android api android-volley

我想从'number'和'step'属性中获取数据,但似乎无法找到解决方案。我是android的初学者,所以非常感谢你的帮助

**这是json ** https://api.myjson.com/bins/135pdu

这是我的代码

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

3 个答案:

答案 0 :(得分:0)

count = recipe_step.getString("number");

步骤编号是一个整数而不是字符串try:

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

答案 1 :(得分:0)

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

对数字使用getInt()

答案 2 :(得分:0)

我相信聪明的工作,而不是长时间的工作。

我更喜欢解析2行代码,而不是编写整个东西来解析json。

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

现在所有值都在您的列表中,在任何地方使用它:)

Google Gson是一个众所周知的解析json的库。只需将小型库implementation 'com.google.code.gson:gson:2.8.4'添加到您的应用级build.gradle。

多数民众赞成,只需要制作能够容纳你的回应变量的pojo(或模型)类。

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

}