【问题标题】:Parsing json data with inconsistent keys解析key不一致的json数据
【发布时间】:2016-11-08 16:44:10
【问题描述】:

如何解析下面数据对象中键不一致的 json 数据?我可以用这样的 json 数据创建模型类吗?如果数据对象中的每个数组都有相同的键,那会很简单。但是使用不同的键我应该如何继续?我可以使用 gson 吗?

注意-键值2016和2015不是固定的,是随机的。数据对象内部可以有更多的随机键值的数组。

{
  "responseCode": 200,
  "responseMessage": "Operation succeeded successfully",
  "data": {
    "2016": [
      [
        {
          "key": "Id",
          "value": "101_202704916"
        },
        {
          "key": "amount",
          "value": "1.48"
        },
        {
          "key": "Type",
          "value": "gchgch"
        }
      ],
      [
          {
          "key": "Id",
          "value": "101_202704916"
        },
        {
          "key": "amount",
          "value": "1.48"
        },
        {
          "key": "Type",
          "value": "gchgch"
        }
      ]
      ],
      "2015": [
      [
         {
          "key": "Id",
          "value": "101_202704916"
        },
        {
          "key": "amount",
          "value": "1.48"
        },
        {
          "key": "Type",
          "value": "gchgch"
        }

      ],
      [
          {
          "key": "Id",
          "value": "101_202704916"
        },
        {
          "key": "amount",
          "value": "1.48"
        },
        {
          "key": "Type",
          "value": "gchgch"
        }

      ]
      ]
  }
  }

【问题讨论】:

    标签: android json parsing gson


    【解决方案1】:

    是的,我认为您可以使用 gson 做类似的事情。我想你要找的是LinkedTreeMap。因此,请尝试以下方式:

    @Expose
    @SerializedName("data")
    private LinkedTreeMap<String, ArrayList<LinkedTreeMap<String, String>>> mT1;
    

    我不完全确定这是否是正确数量的包装列表,因此请根据您的 JSON 模型进行调整。但是我以前用过LinkedTreeMap 来做类似的事情,它就像魔法一样工作。

    【讨论】:

      【解决方案2】:

      试试这个解决方案

      public class KeyModel {
          @SerializedName("key")
          private String key;
          @SerializedName("value")
          private String value;
      
          public String getKey() {
              return key;
          }
      
          public void setKey(String key) {
              this.key = key;
          }
      
          public String getValue() {
              return value;
          }
      
          public void setValue(String value) {
              this.value = value;
          }
      }
      
      
      
      public class DataModel {
          @SerializedName("2016")
          private List<List<KeyModel>> list2016;
          @SerializedName("2015")
          private List<List<KeyModel>> list2015;
      
          public List<List<KeyModel>> getList2016() {
              return list2016;
          }
      
          public void setList2016(List<List<KeyModel>> list2016) {
              this.list2016 = list2016;
          }
      
          public List<List<KeyModel>> getList2015() {
              return list2015;
          }
      
          public void setList2015(List<List<KeyModel>> list2015) {
              this.list2015 = list2015;
          }
      }
      
      public class ResponseModel {
          @SerializedName("responseCode")
          private int responseCode;
          @SerializedName("responseMessage")
          private String responseMessage;
          @SerializedName("data")
          private DataModel data;
      
          public int getResponseCode() {
              return responseCode;
          }
      
          public void setResponseCode(int responseCode) {
              this.responseCode = responseCode;
          }
      
          public String getResponseMessage() {
              return responseMessage;
          }
      
          public void setResponseMessage(String responseMessage) {
              this.responseMessage = responseMessage;
          }
      
          public DataModel getData() {
              return data;
          }
      
          public void setData(DataModel data) {
              this.data = data;
          }
      }
      

      【讨论】:

      • 键值 2016 和 2015 不是固定的,是随机的。数据对象内部可以有更多的 arraylist 具有随机键值。我该如何处理?
      • 数据是一个对象,里面有两个数组..你确定 List>> data;会工作吗?
      【解决方案3】:

      如果你知道有一个键遵循一些顺序,你可以应用逻辑 您可以使用以下方法作为某种检查,

          boolean has(String keyName)
      

      - 如果 JsonObject 的值映射到 keyName,则返回 true。

      【讨论】:

        猜你喜欢
        • 2019-04-07
        • 2013-02-25
        • 1970-01-01
        • 1970-01-01
        • 2019-04-17
        • 2013-10-13
        • 2018-04-23
        • 2017-10-23
        • 2015-11-26
        相关资源
        最近更新 更多