如何解析JSON数据以下数据对象内部不一致的密钥?2016年和2015年的密钥名称不是固定的,并且是随机的。可以有更多的数组数据对象与随机键名称。我可以用这样的json数据创建模型类吗?我可以使用gson吗?使用随机密钥解析json数据(密钥不会始终保持不变)
回答
您无法使用未知键创建模型。如果你想制作模型,那么你必须知道键的名称。 是的,你可以阅读动态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
}
questionMark?它应该是data.getJSONArray(currentDynamicKey); –
@Androidjack替换,我很愚蠢的错误。 –
用途:
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 }
}
}
你也可以试试这个简单的:
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"));
}
}
2016和2015的键名不是固定的,而是随机的。数据对象中可以有更多的数组,并且数据对象中的数组数量不固定。 – Bhuvi
@Bhuvi你可以按照Dharmbir Singh和Sagar Zala的回答,上面的答案也有效 –
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();
}
你可以解析这个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());
}
}
}
- 1. Deserialise JSON与随机密钥
- 2. 使用解密密钥解密数据
- 3. Android Studio - 解析JSON密钥
- 4. 使用随机生成的密钥进行加密和解密?
- 5. 从密钥代码解析密钥
- 6. 用随机密钥循环json
- 7. 用密钥加密和解密数据
- 8. 解密OpenPGP会话密钥
- 9. 数组循环保持一定数量的密钥,并根据元素使用关联密钥替换密钥
- 10. 为什么解密的密钥与加密密钥不一样?
- 11. Simplejson和随机密钥值
- 12. 是AES密钥随机?
- 13. 随机密钥生成
- 14. 密钥保管库密钥与秘密
- 15. 波雷费密码随机密钥(C#)
- 16. 索引多个密钥用于不同密钥组合中的随机查询
- 17. 解析带有未知密钥的JSON?
- 18. 解析JSON字符串动态密钥
- 19. 如何解析JSON没有根密钥
- 20. 用私钥加密的数据只能用公钥解密。
- 21. GSON解析 - 不同类型的密钥
- 22. GSON不返回JSON密钥
- 23. 使用解密密钥加密文件
- 24. GAE用密钥获取数据使用JDO使用密钥
- 25. ndb.StructuredProperty不保存密钥?
- 26. C#反序列化的Facebook JSON开始随机密钥
- 27. json解码密钥类型
- 28. 编码/解码JSON密钥?
- 29. 使用AES128 CTR在给定密钥的iOS中加密和解密随机NSString
- 30. WSO2使用公钥私钥加密/解密密码(字符串)
你可以参考这个答案,http://stackoverflow.com/a/31025810/3940292 –