gpt4 book ai didi

java - 尝试创建 JSONArray 时出现 JSONException

转载 作者:行者123 更新时间:2023-11-30 06:36:39 25 4
gpt4 key购买 nike

请告知为什么我在尝试创建 JSONArray 实例时遇到异常?

String task = "{'menu': { 'id': 'file', 'value': 'File', 'popup': { 'menuitem': [ {'value': 'New', 'onclick': 'CreateNewDoc()'}, {'value': 'Open', 'onclick': 'OpenDoc()'}, {'value': 'Close', 'onclick': 'CloseDoc()'}] }}}";
try {
JSONObject tmp = new JSONObject(task);
js = tmp.getJSONArray("menuitem"); // exception fires here
} catch(JSONException e) {
e.printStackTrace();
}

获取异常

01-03 16:12:17.926: WARN/System.err(5999): org.json.JSONException: No value for menuitem

最佳答案

'menuitem' 不是 tmp 的子项。试试这个:

js = tmp.getJSONObject("menu").getJSONObject("popup").getJSONArray("menuitem");

关于java - 尝试创建 JSONArray 时出现 JSONException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4585162/

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