[英]JSONArray cannot be converted to JSONObject in java
I want to get an json object from this string coming from this JSON file: 我想从来自此JSON文件的字符串中获取一个json对象:
{
"categories": {
"category": [
{
"label": "Collaboration",
"thumbnail": "http://mywebite.com/dfsv",
"description": "...",
"contents": {
"@attributes": {
"count": "79"
}
},
"@attributes": {
"id": "ZSQYN2"
}
},
{
"label": "Communication",
"thumbnail": "http://mywebite.com/dfsv",
"description": "...",
"contents": {
"@attributes": {
"count": "43"
}
},
"@attributes": {
"id": "uGQ9MC"
}
}
],
"@attributes": {
"page": "12",
"limit": "0",
"count": "111",
"lang": "en"
}
}
} }
I'm using this code : 我正在使用此代码:
JSONObject obj = new JSONObject(myString);
And I got this error message: 我得到了这个错误信息:
Value ... of type org.json.JSONArray cannot be converted to JSONObject
org.json.JSONArray类型的值...无法转换为JSONObject
And if I do : 如果我这样做:
JSONArray obj = new JSONArray(myString);
I got this error message: 我收到此错误消息:
Value ... of type org.json.JSONObject cannot be converted to JSONArray
org.json.JSONObject类型的值...无法转换为JSONArray
... ...
Do you have any idea ? 你有什么主意吗 ?
JSONArray
and JSONObject
are two different things, and should not be type compatible with one another, so these errors make sense. JSONArray
和JSONObject
是两个不同的事物,并且不应在类型上相互兼容,因此这些错误是有道理的。 This is a JSONArray: 这是一个JSONArray:
["foo", "bar", "baz"]
This is a JSONObject: 这是一个JSONObject:
{ "foo" : "bar", "baz" : "quux" }
You may be looking for JSONElement
, which in APIs like Google's GSON is the common superclass between both arrays and objects. 您可能正在寻找
JSONElement
,在像Google的GSON这样的API中, JSONElement是数组和对象之间的通用超类。
Under the Android JSON API they're also not type compatible. 在Android JSON API下,它们也不兼容类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.