gpt4 book ai didi

android - 在不更改键顺序的情况下将 json 字符串转换为 json 对象

转载 作者:行者123 更新时间:2023-11-29 14:14:03 26 4
gpt4 key购买 nike

我有一个如下所示的json字符串

{
"college": {
"id": "RPD4007",
"address": "#302, 1 st cross"
},
"deparment": {
"department1": {
"name": {
"maths": {
"chapter": 1,
"name": "algebra",
"module_id": "01"
},
"electronics": {
"chapter": 1,
"name": "ec",
"module_id": "01"
}
}
},
"department2": {
"name": {
"english": {
"chapter": 2,
"name": "algebra",
"module_id": "02"
},
"electrical": {
"chapter": 2,
"name": "algebra",
"module_id": "02"
}
}
}
}
}

我已经尝试将这个 json sring 转换为 json 对象,

string json_string = EntityUtils.toString(response.getEntity());
jObj = new JSONObject(json_string);//json object
JSONObject object = jobj.getJSONObject("college");

但我得到的 jobj 输出是 json 字符串的相反顺序。如下所示,

{
"college": {
"id": "RPD4007",
"address": "#302, 1 st cross"
},
"deparment": {
"department2": {
"name": {
"electrical": {
"chapter": 2,
"name": "algebra",
"module_id": "02"
},
"english": {
"chapter": 2,
"name": "algebra",
"module_id": "02"
}
}
},
"department1": {
"name": {
"electronics": {
"chapter": 1,
"name": "ec",
"module_id": "01"
},
"maths": {
"chapter": 1,
"name": "algebra",
"module_id": "01"
}
}
}
}
}

如何按原来的顺序得到它?

最佳答案

我认为您制作了两次 jsonObject。做这样的事情你有包含 JSON 数据的字符串为你所做的做一个对象

string json_string = EntityUtils.toString(response.getEntity());
jObj = new JSONObject(json_string);//json object

然后使用

进行循环以获取该对象内部的对象
 String college = jobj.getString("college");

关于android - 在不更改键顺序的情况下将 json 字符串转换为 json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32989494/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com