2016-11-09 19 views
0

如何解析JSON数据以下数据对象内部不一致的密钥?2016年和2015年的密钥名称不是固定的,并且是随机的。可以有更多的数组数据对象与随机键名称。我可以用这样的json数据创建模型类吗?我可以使用gson吗?使用随机密钥解析json数据(密钥不会始终保持不变)

+0

你可以参考这个答案,http://stackoverflow.com/a/31025810/3940292 –

回答

2

您无法使用未知键创建模型。如果你想制作模型,那么你必须知道键的名称。 是的,你可以阅读动态JSON像下面的代码这样

JSONObject data = response.getJSONObject("data");// here response is server response 
Iterator keys = data.keys(); 

while(keys.hasNext()) { 
    // loop to get the dynamic key 
    String key = (String)keys.next(); 

    // get the value of the dynamic key 
    JSONArray value = data.getJSONArray(key); 

    // do your more stuff here 
} 
+0

questionMark?它应该是data.getJSONArray(currentDynamicKey); –

+0

@Androidjack替换,我很愚蠢的错误。 –

1

用途:

JSONObject jsonO = new JSONObject(jsonString); 
    int responseCode=jsonO.getInt("responseCode"); 
    String responseMessage=jsonO.getString("responseMessage"); 

    JSONObject jsondata=jsonO.getJSONObject("data"); 

    for (String key : jsondata.keys()) { 
     Object o = jsondata.get(key) 
     if (o instanceof JSONArray) { 
      JSONArray jsonA = (JSONArray) o; 
      int muberOfItems = jsonA.length(); 
      for (int i = 0; i < muberOfItems; i++) { 
       //parse your Data 
      } 
     } else { //must be some other value } 

     } 
    } 
0

你也可以试试这个简单的:

String response = "YOUR RESPONSE"; 

    JSONObject resObj = new JSONObject(response); 
    JSONObject jsonObject = resObj.getJSONObject("data"); 
    HashMap<String, String> hashMap2015 = new HashMap<>(); 
    HashMap<String, String> hashMap2016 = new HashMap<>(); 

    if (jsonObject.has("2015")) { 
     JSONArray jsonArray2015 = jsonObject.getJSONArray("2015"); 

     for (int i = 0; i < jsonArray2015.length(); i++) { 
      JSONObject jsonObject1 = jsonArray2015.getJSONObject(i); 
      hashMap2015.put(jsonObject1.getString("key"), jsonObject1.getString("value")); 
     } 
    } 

    if (jsonObject.has("2016")) { 
     JSONArray jsonArray2016 = jsonObject.getJSONArray("2016"); 
     for (int i = 0; i < jsonArray2016.length(); i++) { 
      JSONObject jsonObject1 = jsonArray2016.getJSONObject(i); 
      hashMap2016.put(jsonObject1.getString("key"), jsonObject1.getString("value")); 
     } 
    } 
+0

2016和2015的键名不是固定的,而是随机的。数据对象中可以有更多的数组,并且数据对象中的数组数量不固定。 – Bhuvi

+0

@Bhuvi你可以按照Dharmbir Singh和Sagar Zala的回答,上面的答案也有效 –

0
JSONObject object = new JSONObject(response); 
try{ 
    JSONObject data = new JSONObject(object.getJSONObject("data")); 
    JSONArray arr2015 = new JSONArray(data); 
    for(int ePos = 0; ePos < arr2015.lenght; ePos ++){ 
      JSONObject insideArr = jsonArray.getJSONObject(ePos); 
      String key = insideAr.getString("key") 
      String value = insideAr.getString("value") 
      //add this values to your object 

    } 
    JSONArray arr2016 = new JSONArray(data); 
    for(int ePos = 0; ePos < arr2016.lenght; ePos ++){ 
      JSONObject insideArr = jsonArray.getJSONObject(ePos); 
      String key = insideAr.getString("key") 
      String value = insideAr.getString("value") 
      //add this values to your object 
    } 

}catch (JSONException e){ 
    return e.getMessage(); 
} 
0

你可以解析这个GSON。这为我工作,

POJO类

ParsedData.java

public class ParsedData { 

    private Integer responseCode; 
    private String responseMessage; 
    Map<String, List<List<Item>>> data = new HashMap<String, List<List<Item>>>(); 

    public Integer getResponseCode() { 
     return responseCode; 
    } 

    public void setResponseCode(Integer responseCode) { 
     this.responseCode = responseCode; 
    } 

    public String getResponseMessage() { 
     return responseMessage; 
    } 

    public void setResponseMessage(String responseMessage) { 
     this.responseMessage = responseMessage; 
    } 

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

    public void setData(Map<String, List<List<Item>>> data) { 
     this.data = data; 
    } 
} 

Item.java

public class Item { 
    private String key; 
    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; 
    } 
} 

GSON代码

String data = "{\n" + 
     "\t\"responseCode\": 200,\n" + 
     "\t\"responseMessage\": \"Operation succeeded successfully\",\n" + 
     "\t\"data\": {\n" + 
     "\t\t\"2016\": [\n" + 
     "\t\t\t[{\n" + 
     "\t\t\t\t\"key\": \"Id\",\n" + 
     "\t\t\t\t\"value\": \"101_202704916\"\n" + 
     "\t\t\t}, {\n" + 
     "\t\t\t\t\"key\": \"amount\",\n" + 
     "\t\t\t\t\"value\": \"1.48\"\n" + 
     "\t\t\t}, {\n" + 
     "\t\t\t\t\"key\": \"Type\",\n" + 
     "\t\t\t\t\"value\": \"gchgch\"\n" + 
     "\t\t\t}],\n" + 
     "\t\t\t[{\n" + 
     "\t\t\t\t\"key\": \"Id\",\n" + 
     "\t\t\t\t\"value\": \"101_202704916\"\n" + 
     "\t\t\t}, {\n" + 
     "\t\t\t\t\"key\": \"amount\",\n" + 
     "\t\t\t\t\"value\": \"1.48\"\n" + 
     "\t\t\t}, {\n" + 
     "\t\t\t\t\"key\": \"Type\",\n" + 
     "\t\t\t\t\"value\": \"gchgch\"\n" + 
     "\t\t\t}]\n" + 
     "\t\t],\n" + 
     "\t\t\"2015\": [\n" + 
     "\t\t\t[{\n" + 
     "\t\t\t\t\t\"key\": \"Id\",\n" + 
     "\t\t\t\t\t\"value\": \"101_202704916\"\n" + 
     "\t\t\t\t}, {\n" + 
     "\t\t\t\t\t\"key\": \"amount\",\n" + 
     "\t\t\t\t\t\"value\": \"1.48\"\n" + 
     "\t\t\t\t}, {\n" + 
     "\t\t\t\t\t\"key\": \"Type\",\n" + 
     "\t\t\t\t\t\"value\": \"gchgch\"\n" + 
     "\t\t\t\t}\n" + 
     "\n" + 
     "\t\t\t],\n" + 
     "\t\t\t[{\n" + 
     "\t\t\t\t\t\"key\": \"Id\",\n" + 
     "\t\t\t\t\t\"value\": \"101_202704916\"\n" + 
     "\t\t\t\t}, {\n" + 
     "\t\t\t\t\t\"key\": \"amount\",\n" + 
     "\t\t\t\t\t\"value\": \"1.48\"\n" + 
     "\t\t\t\t}, {\n" + 
     "\t\t\t\t\t\"key\": \"Type\",\n" + 
     "\t\t\t\t\t\"value\": \"gchgch\"\n" + 
     "\t\t\t\t}\n" + 
     "\n" + 
     "\t\t\t]\n" + 
     "\t\t]\n" + 
     "\t}\n" + 
     "}"; 

ParsedData data1 = new Gson().fromJson(data, ParsedData.class); 
for (String key : data1.getData().keySet()) { 
    List<List<Item>> items = data1.getData().get(key); 
    for (List<Item> item : items) { 
     for (Item item1 : item) { 
      Log.e("TAG", item1.getKey() + " : " + item1.getValue()); 
     } 
    } 
}