设置JSON输入值动态

问题描述 投票:1回答:1

我试图动态地将JSON值设置为Array List。如果它是恒定值但是如何在运行中设置该值,则它按预期工作。非常感谢您的回复。

我从URL中获取JSON值

 https://www.abc.test/test.json

整个源代码

https://github.com/bpncool/SectionedExpandableGridRecyclerView

__JSON输入:__

  {
  "objectsArray": [
    {
      "name": "Apple Products",
      "Data": {
        "type1": "iPhone",
        "type2": "iPad",
        "type3": "iPod",
        "type4": "iMac"
      }
    },
    {
      "name": "Companies",
      "Data": {
        "type1": "LG",
        "type2": "Apple",
        "type3": "Samsung"
      }
    }
  ]
}

Java静态代码

ArrayList<Item> arrayList = new ArrayList<>();
arrayList.add(new Item("iPhone", 0));
arrayList.add(new Item("iPad", 1));
arrayList.add(new Item("iPod", 2));
arrayList.add(new Item("iMac", 3));
sectionedExpandableLayoutHelper.addSection("Apple Products", arrayList);
arrayList = new ArrayList<>();
arrayList.add(new Item("LG", 0));
arrayList.add(new Item("Apple", 1));
arrayList.add(new Item("Samsung", 2));
sectionedExpandableLayoutHelper.addSection("Companies", arrayList);
sectionedExpandableLayoutHelper.notifyDataSetChanged()
android arraylist
1个回答
0
投票

您的输入Json结构有一个小问题。它应该包含一个JsonObjects数组,每个都有"name""Data"属性。

{
  "objectsArray": [
    {
      "name": "Apple Products",
      "Data": {
        "type1": "iPhone",
        "type2": "iPad",
        "type3": "iPod",
        "type4": "iMac"
      }
    },
    {
      "name": "Companies",
      "Data": {
        "type1": "LG",
        "type2": "Apple",
        "type3": "Samsung"
      }
    }
  ]
}

•你的"Data"的结构就像一个Map,所以我把它模型化为Map

import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;

import java.util.List;
import java.util.Map;

public class ProductsResponse {

    @SerializedName("objectsArray")
    private List<MyObject> objectsArray;

    public List<MyObject> getObjectsArray() {
        return objectsArray;
    }

    public static class MyObject {

        @SerializedName("name")
        private String name;

        @SerializedName("Data")
        private Map<String, String> data;

        public String getName() {
            return name;
        }

        public Map<String, String> getData() {
            return data;
        }

    }

    public String serialize() {
        return new Gson().toJson(this, ProductsResponse.class);
    }

    public static ProductsResponse deserialize(String jsonString) {
        if (jsonString == null)
            return new ProductsResponse();
        else {
            return new Gson().fromJson(jsonString, ProductsResponse.class);
        }
    }

}

•其反序列化结果是:

enter image description here

•最后,您可以按以下方式填写列表:

ProductsResponse products = ProductsResponse.deserialize(jsonString);
for (MyObject myObject : products.getObjectsArray()) {
    ArrayList<Item> arrayList = new ArrayList<>();
    int i = 0;
    for (Map.Entry<String, String> entry : myObject.getData().entrySet()) {
        arrayList.add(new Item(entry.getValue(), i++));
    }
    sectionedExpandableLayoutHelper.addSection(myObject.getName(), arrayList);
}
sectionedExpandableLayoutHelper.notifyDataSetChanged();
© www.soinside.com 2019 - 2024. All rights reserved.