【问题标题】:How to resolve complex JSON and put it in list into list using Retrofit如何使用 Retrofit 解析复杂的 JSON 并将其放入列表中
【发布时间】:2019-04-20 21:43:49
【问题描述】:

我有如下所示的 JSON

{
"status" : "succesfull",
"items": [
    {
        "id" : "1",
        "name": "apple",
        "counrty": "USA"
    },
    {
        "id": "2",
        "name": "banana",
        "country": "Jamaica"
    },
    {
        "id":"3",
        "name": "potatoes",
        "counrty": "Belarus"
    },
    ...
   ]
  }

比方说,国家是多种多样的,我不知道哪些国家会在列表中

我需要列出如下清单:

任何解决方案将不胜感激

public class Response {
  String status;
  ArrayList<Items> items;      
}



public class Items{
   String id;
   String name;
   String country;
}

【问题讨论】:

标签: java android json android-studio retrofit2


【解决方案1】:

您可以这样做,(抱歉格式错误) 确保在结构类中添加 getter/setter 方法。

JSONObject jsonObj = new JSONObject(responseString);
Response response = new Response();

if(jsonObj.has("status")){ 
response.setStatus(jsonObj.getString("status"));
}

if(jsonObj.has("items")) {
 JSONArray array = jsonObj.getJSONArray("items");
 ArrayList<Items> list =new ArrayList();

for(i=0;i<array.length;i++){ 
     JSONObject json = array.get(i); 
     Items item = new Items();
     item.setId(json.getString("id"));
     item.setName(json.getString("name"));
     item.setCountry(json.getString("country"));
     list.add(item);
}
}
response.setItems(list);

【讨论】:

  • 在您的回答中是否有任何具体原因不建议 Gson、Jackson 或 Moshi 等 JSON 解析库?我不是说你的程序是错误的或者不应该被遵循,但它会使代码变得很长,如果我们想在 JSON 结构中更改任何 key,那么这种代码风格会使开发人员的生活变得更糟。 :)
  • 使用库的缺点是当你应用proguard规则并且想要混淆你的代码时,这些类将被排除,因为这些库的proguard规则可能不同,这本身就是一个巨大的安全原因目的。
【解决方案2】:

你好,就在前面,

将您的 JSON 放入任何 JSON 解析器,例如 http://www.jsonschema2pojo.org/
比您需要选择 Parcelable / Serializable ,您将获得自动生成的 POJO 类。
比您必须使用 okHttp / gson 设置改造实例,创建 dao 接口,您可以在其中实现所有方法来调用您的 JSON。

【讨论】:

    【解决方案3】:

    您的 POJO 将如下所示:

    public class Response{
    private List<ItemsItem> items;
    private String status;
    
    public void setItems(List<ItemsItem> items){
        this.items = items;
    }
    
    public List<ItemsItem> getItems(){
        return items;
    }
    
    public void setStatus(String status){
        this.status = status;
    }
    
    public String getStatus(){
        return status;
    }
    
    
    class ItemsItem{
        private String counrty;
        private String name;
        private String id;
        private String country;
    
        public void setCounrty(String counrty){
            this.counrty = counrty;
        }
    
        public String getCounrty(){
            return counrty;
        }
    
        public void setName(String name){
            this.name = name;
        }
    
        public String getName(){
            return name;
        }
    
        public void setId(String id){
            this.id = id;
        }
    
        public String getId(){
            return id;
        }
    
        public void setCountry(String country){
            this.country = country;
        }
    
        public String getCountry(){
            return country;
         }
    
     } }
    

    使用带有 TextView 和 ListView 的 CustomLayout XML 创建 RecyclerView。

    在 RecyclerViewAdapter 类上只设置 Title 和 Lists

    recyclerView 的自定义布局如下所示

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Title Goes here" />
    
       <ListView
            android:layout_width="match_parent"
            android:layout_height="@dimen/_150sdp"/></LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-05
      • 1970-01-01
      • 2020-09-29
      • 2019-01-19
      相关资源
      最近更新 更多