【问题标题】:I can parse JsonObject but having some difficulty with JsonArray using volley library我可以解析 JsonObject 但使用 volley 库对 JsonArray 有一些困难
【发布时间】:2018-10-29 06:19:44
【问题描述】:

我想从 '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());
                }

【问题讨论】:

    标签: android api android-volley


    【解决方案1】:

    count = recipe_step.getString("number");

    步数是整数而不是字符串试试:

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

    【讨论】:

      【解决方案2】:
        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()

      【讨论】:

        【解决方案3】:

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

        我更喜欢 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;
        }
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-05-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多