使用不一致的密钥解析json数据

时间:2016-11-08 16:44:10

标签: android json parsing gson

如何在数据对象内部用不一致的键解析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"
        }

      ]
      ]
  }
  }

3 个答案:

答案 0 :(得分:0)

是的我认为你可以用gson做同样的事情。我认为你要找的是LinkedTreeMap。所以尝试一下以下几点:

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

我不完全确定这是否是适量的包装列表,因此请根据您的JSON模型进行调整。但我之前使用过LinkedTreeMap之类的东西,就像魔法一样。

答案 1 :(得分:0)

尝试此解决方案

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;
    }
}

答案 2 :(得分:0)

如果您知道按键有一些顺序,则可以应用逻辑 并且您可以使用以下方法作为某种检查,

    boolean has(String keyName)

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