设置JSON输入值动态

时间:2019-01-25 23:20:52

标签: android arraylist

我正在尝试将JSON值动态设置为数组列表。如果它是恒定值,它将按预期工作,但如何动态设置此值。非常感谢您的回复。

我正在从类似的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()

1 个答案:

答案 0 :(得分:0)

您的输入Json结构有一个小问题。它应包含JsonObject个数组,每个数组都具有"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();