gpt4 book ai didi

android - 错误 "is not a JSONObject"

转载 作者:太空狗 更新时间:2023-10-29 16:38:11 25 4
gpt4 key购买 nike

我有 JSON 字符串:

{ "products" : [ { "created_at" : "2014-04-09 23:10:15",
"name" : "tessss",
"pid" : "1",
"price" : "10.00",
"updated_at" : "0000-00-00 00:00:00"
},
{ "created_at" : "2014-04-10 21:14:51",
"name" : "avgs",
"pid" : "2",
"price" : "20.00",
"updated_at" : "0000-00-00 00:00:00"
}
],
"success" : 1
}

我的代码是(行是 JSON 字符串):

JSONObject nodeRoot  = new JSONObject(line); 

// Creating a sub-JSONObject from another JSONObject
JSONObject nodeStats = nodeRoot.getJSONObject("products");

// Getting the value of a attribute in a JSONObject
String sSDR = nodeStats.getString("name");
System.out.println(sSDR);

当我运行时出现错误 JSONObject["products"] is not a JSONObject.

最佳答案

{代表JSONObject,[代表JSONArray

在您的 json 产品中代表 JSONArray 而不是 Object

像这样获取名称字符串。

JSONObject nodeRoot  = new JSONObject(line); 
JSONArray nodeStats = nodeRoot.getJSONArray("products");
for(int i=0; i < nodeStats.length();i++){
String sSDR = nodeStats.getJSONObject(i).getString("name");
}

关于android - 错误 "is not a JSONObject",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23011779/

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