gpt4 book ai didi

java - 使用 JSONObject 打印键值未​​按预期工作

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

我试图打印出下面 JSON 中的所有键值,但无论出于何种原因,它只为我打印了第一组值。

如何打印idValue字符串中的所有键值?

编辑:我现在收到错误:

意外错误:JSONObject["submenu"] 不是字符串。

代码

import com.google.common.collect.LinkedListMultimap;
import com.google.common.collect.Multimap;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.Map;

public class testing {
static Multimap<String, String> allMappedKeyValues = LinkedListMultimap.create();

public static void main(String[] args) {

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\"}]}]";

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)
{
String key = keys.getString(j);
Object value = object.get(key);

if (value instanceof JSONArray) {

JSONArray array1 = (JSONArray) value;

for (int k = 0; k < array1.length(); k++) {
JSONObject object1 = array1.getJSONObject(k);

JSONArray keys1 = object1.names();

String key2 = keys1.getString(k);
String value2 = object1.getString(key2);
String title2 = object1.getString("title");
// System.out.println("-key :" + key2 + "\n-value " + value2 + "\n-title :" + title2);

if (key2.contains("amid")) {
allMappedKeyValues.put(value2 + "__title", title2);
}
}
} else {
String key1 = keys.getString(j);
String value1 = object.getString(key1);
String title1 = object.getString("title");
// System.out.println("-key :" + key1 + "\n-value " + value1 + "\n-title :" + title1);

if (key.equals("amid")) {
allMappedKeyValues.put(value1 + "__title", title1);
}
}
}
}

for (Map.Entry<String, String> entry : allMappedKeyValues.entries()) {
System.out.println("This is the key-" + entry.getKey() + " value-" + entry.getValue());
}
}
}

JSON

[{"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"}]}]

输出

  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

最佳答案

您不需要替换 [],您的 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

关于java - 使用 JSONObject 打印键值未​​按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52639520/

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