3 回答
TA贡献1966条经验 获得超4个赞
您不需要替换[],您的 JSON 格式正确。您实际上可以JSONArray直接从字符串创建,
String idValue = "[{\"link\":\"/us_new/en/home\",\"amid\":\"1__home\",\"title\":\"Home\"},{\"link\":\"/us_new/en/home/diagnosis\",\"amid\":\"2__diagnosis\",\"title\":\"Diagnosis\"},{\"link\":\"/us_new/en/home/treatment\",\"amid\":\"3__loss\",\"title\":\"loss\"}]";
JSONArray objects = new JSONArray(idValue);
for(int i = 0; i < objects.length(); i++) {
JSONObject jsonObject = objects.getJSONObject(i);
Iterator<String> keys = jsonObject.keys();
while(keys.hasNext()) {
String key = keys.next();
System.out.printf("key : %s | value : %s\n", key, jsonObject.get(key));
}
}
输出 :
key : link | value : /us_new/en/home
key : amid | value : 1__home
key : title | value : Home
key : link | value : /us_new/en/home/diagnosis
key : amid | value : 2__diagnosis
key : title | value : Diagnosis
key : link | value : /us_new/en/home/treatment
key : amid | value : 3__loss
key : title | value : loss
TA贡献1844条经验 获得超8个赞
您可以使用以下递归函数“parseJsonArray”来解析嵌套的 jsonArrays。
String idValue = "[{\"link\": \"/us_new/en/home\",\"amid\": \"1__home\",\"title\": \"Home\"}, {\"link\": \"/us_new/en/home/diagnosis\",\"amid\": \"2__diagnosis\",\"title\": \"Diagnosis\"}, {\"link\": \"/us_new/en/home/loss\",\"amid\": \"3__loss\",\"title\": \"loss\",\"submenu\": [{\"amid\": \"4__quiz\",\"name\": \"quiz\",\"title\": \"quiz\"},{\"amid\": \"5__questions\",\"name\": \"questions\",\"title\": \"Questions\"}]}]";
parseJsonArray(idValue);
private void parseJsonArray(String jsonString) throws JSONException {
System.out.println("jsonString: " + jsonString);
JSONArray objects = new JSONArray(jsonString);
JSONArray nestedJsonArray;
JSONObject nestedJsonObject;
for(int i = 0; i < objects.length(); i++) {
JSONObject jsonObject = objects.getJSONObject(i);
Iterator<String> keys = jsonObject.keys();
while(keys.hasNext()) {
String key = keys.next();
Object nestedObject = jsonObject.get(key);
if (nestedObject instanceof JSONArray) {
System.out.println("it's an array: " + key);
nestedJsonArray = (JSONArray)nestedObject;
parseJsonArray(nestedJsonArray.toString());
}
else if (nestedObject instanceof JSONObject) {
nestedJsonObject = (JSONObject)nestedObject;
System.out.printf("key : %s | value : %s | title : %s\n", key, nestedJsonObject.getString(key), nestedJsonObject.getString("title"));
}
else {
System.out.printf("key : %s | value : %s | title : %s\n", key, jsonObject.getString(key), jsonObject.getString("title"));
}
}
}
TA贡献1757条经验 获得超8个赞
您只读取了第一个 JSON 对象 ( {\"link\":\"/us_new/en/home\",\"amid\":\"1__home\",\"title\":\"Home\"})。为了读取所有 3 个对象,首先将 JSON 字符串更改为数组(添加缺少的[和]):
String idValue = "[{\"link\":\"/us_new/en/home\",\"amid\":\"1__home\",\"title\":\"Home\"},{\"link\":\"/us_new/en/home/diagnosis\",\"amid\":\"2__diagnosis\",\"title\":\"Diagnosis\"},{\"link\":\"/us_new/en/home/treatment\",\"amid\":\"3__loss\",\"title\":\"loss\"}]";
然后遍历数组:
JSONArray array = new JSONArray(idValue);
for (int i = 0; i < array.length (); i++) {
JSONObject object = array.getJSONObject (i);
JSONArray keys = object.names();
for (int j = 0; j < keys.length(); ++j) {
System.out.println("Entered loop :"+j);
String key = keys.getString(j);
String value = object.getString(key);
String title = object.getString("title");
System.out.println("-key :" + key + "\n-value " + value + "\n-title :" + title);
}
}
输出:
Entered loop :0
-key :link
-value /us_new/en/home
-title :Home
Entered loop :1
-key :amid
-value 1__home
-title :Home
Entered loop :2
-key :title
-value Home
-title :Home
Entered loop :0
-key :link
-value /us_new/en/home/diagnosis
-title :Diagnosis
Entered loop :1
-key :amid
-value 2__diagnosis
-title :Diagnosis
Entered loop :2
-key :title
-value Diagnosis
-title :Diagnosis
Entered loop :0
-key :link
-value /us_new/en/home/treatment
-title :loss
Entered loop :1
-key :amid
-value 3__loss
-title :loss
Entered loop :2
-key :title
-value loss
-title :loss
添加回答
举报