Android 使用随机键解析json数据(键在整个过程中不保持不变)
如何解析数据对象中键不一致的json数据?键名称2016和2015不是固定的,是随机的。数据对象中可以有更多具有随机键名称的数组。我可以用这样的json数据创建一个模型类吗?我可以使用gson吗?你不能用未知键创建模型。如果你想做模型,那么你必须知道钥匙的名字。 是的,您可以像这样读取动态jsonAndroid 使用随机键解析json数据(键在整个过程中不保持不变),android,json,parsing,gson,Android,Json,Parsing,Gson,如何解析数据对象中键不一致的json数据?键名称2016和2015不是固定的,是随机的。数据对象中可以有更多具有随机键名称的数组。我可以用这样的json数据创建一个模型类吗?我可以使用gson吗?你不能用未知键创建模型。如果你想做模型,那么你必须知道钥匙的名字。 是的,您可以像这样读取动态json JSONObject data = response.getJSONObject("data");// here response is server response Iterator keys =
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
}
无法使用未知关键点创建模型。如果你想做模型,那么你必须知道钥匙的名字。 是的,您可以像这样读取动态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
}
使用以下代码:
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 }
}
}
JSONObject jsonO=新的JSONObject(jsonString);
int responseCode=jsonO.getInt(“responseCode”);
字符串responseMessage=jsonO.getString(“responseMessage”);
JSONObject jsondata=jsonO.getJSONObject(“数据”);
for(字符串键:jsondata.keys()){
对象o=jsondata.get(键)
if(o JSONArray的实例){
JSONArray jsonA=(JSONArray)o;
int muberOfItems=jsonA.length();
对于(int i=0;i
使用以下代码:
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 }
}
}
JSONObject jsonO=新的JSONObject(jsonString);
int responseCode=jsonO.getInt(“responseCode”);
字符串responseMessage=jsonO.getString(“responseMessage”);
JSONObject jsondata=jsonO.getJSONObject(“数据”);
for(字符串键:jsondata.keys()){
对象o=jsondata.get(键)
if(o JSONArray的实例){
JSONArray jsonA=(JSONArray)o;
int muberOfItems=jsonA.length();
对于(int i=0;i
您也可以尝试以下简单方法:
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"));
}
}
String response=“您的响应”;
JSONObject ResObject=新JSONObject(响应);
JSONObject JSONObject=resObj.getJSONObject(“数据”);
HashMap hashMap2015=新建HashMap();
HashMap hashMap2016=新建HashMap();
如果(jsonObject.has(“2015”)){
JSONArray jsonArray2015=jsonObject.getJSONArray(“2015”);
for(int i=0;i
您也可以尝试以下简单方法:
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"));
}
}
String response=“您的响应”;
JSONObject ResObject=新JSONObject(响应);
JSONObject JSONObject=resObj.getJSONObject(“数据”);
HashMap hashMap2015=新建HashMap();
HashMap hashMap2016=新建HashMap();
如果(jsonObject.has(“2015”)){
JSONArray jsonArray2015=jsonObject.getJSONArray(“2015”);
for(int i=0;i
JSONObject object=新的JSONObject(响应);
试一试{
JSONObject数据=新的JSONObject(object.getJSONObject(“数据”));
JSONArray arr2015=新JSONArray(数据);
对于(int ePos=0;ePos
JSONObject object=新的JSONObject(响应);
试一试{
JSONObject数据=新的JSONObject(object.getJSONObject(“数据”));
JSONArray arr2015=新JSONArray(数据);
对于(int ePos=0;ePos
您可以使用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;
}
}
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());
}
}
}
String data=“{\n”+
\t\“响应代码\”:200\n+
\t\“responseMessage\:\“操作成功\”,\n+
“\t\”数据\“:{\n”+
“\t\t\“2016\”:[\n”+
“\t\t\t[{\n”+
“\t\t\t\t\”键\“:\”Id\“,\n”+
“\t\t\t\t\”值“:\”101\U 202704916\“\n”+
\t\t\t},{\n+
“\t\t\t\t\”关键字“:\”金额“,\n”+