作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有 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/
我是一名优秀的程序员,十分优秀!