【问题标题】:how to parse json in RecyclerView with android?如何使用 android 解析 RecyclerView 中的 json?
【发布时间】:2021-07-07 08:30:57
【问题描述】:

我从 api 获取 json :

{
    "SuccessInfo": [       
        {
            "key": "clothes,color",
            "value": [
                "pink",
                "green",
                "yellow",
                "white",
                "blue",
                "black"
            ]
        },
        {
            "key": "clothes,type",
            "value": [
                "female",
                "male",
                "child"
            ]
        }

会计json,我设计了一个类:

public class Clothes{
   
    public static class SuccessInfoBean{
           
        private String key;
        private List<String> value;
           
        public SuccessInfoBean(String key , List<String> value){
            this.key = key;
            this.value = value;
        }

    }
}

在MainActivity中,我想知道如何解析“值”?

    List<Clothes.SuccessInfoBean> infoBean = new ArrayList<>();
    List<String> stringList = new ArrayList<>();
    JSONObject jsonObject2 = new JSONObject(myJson2.toString());
    JSONArray  jsonArray = jsonObject2.getJSONArray("SuccessInfoBean");
     
    for (int j =0; j < jsonArray.length(); j++){
       JSONObject object2 = jsonArray.getJSONObject(j);
       String key2 = object2.getString("key");
       stringList.add(object2.getString("value"));
       

我的用户 infoBean.add(key,stringList) 会出错,

哪一部分错了?

【问题讨论】:

标签: android json android-recyclerview


【解决方案1】:
  String key2 = object2.getString("key");
  JSONArray jsonValue = object2.getJSONArray("value");
  for (int i=0; i < jsonValue.length();i ++){
    stringList.add(jsonValue.getString(i)
  }

【讨论】:

    【解决方案2】:

    您无需在适配器类中使用回收器视图,而是查看此 expandable-recycler-view https://github.com/thoughtbot/expandable-recycler-view

    在其中创建您的另一个视图。 因此,您的复选框将是子视图/布局,而 Clothes 和颜色将是父布局。

    【讨论】:

      猜你喜欢
      • 2021-07-07
      • 2021-04-01
      • 2014-05-10
      • 2021-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多