如何在数据对象内部使用不一致的密钥解析json数据?2016年和2015年的密钥名称不是固定的并且是随机的。数据对象中可以有更多数组,具有随机密钥名称。我可以创建一个模型类这样的json数据?我可以用gson吗?
答案 0 :(得分: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
}
答案 1 :(得分: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 }
}
}
答案 2 :(得分:0)
You can also try this Simple :
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"));
}
}
答案 3 :(得分: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();
}
答案 4 :(得分: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());
}
}
}