java JSONObject JSON 数组长度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25489564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSONObject JSON array length
提问by spiderman
I am reading the below XML in java, and converting to JSONObject
[org.json.JSONObject
].
and appending the metadata and returning as JSON.
我正在用 java 阅读下面的 XML,并转换为JSONObject
[ org.json.JSONObject
]。并附加元数据并作为 JSON 返回。
Problem:
JSONObject jsonObj = service.getMyData();
// custom methods and invocation
问题:
JSONObject jsonObj = service.getMyData();
// 自定义方法和调用
int count = jsonObj.length();
// length is 1
int count = jsonObj.length();
// 长度为 1
How do I take the correct count from this jsonObj? . The correct count I am referring to is 2 in this case. [2 books]
我如何从这个 jsonObj 中获取正确的计数?. 在这种情况下,我所指的正确计数是 2。[2本书]
What I tried?
我尝试了什么?
(1) System.out.println(jsonObj.length());
// prints 1
(1) System.out.println(jsonObj.length());
// 打印 1
API says: length() Get the number of keys stored in the JSONObject.
API 说:length() 获取存储在 JSONObject 中的键的数量。
(2) Since number of keys is returned as 1, there is no point on trying the below one, still I tried
and got the error of org.json.JSONException: JSONObject["User"] not found.
(2) 由于键数返回为 1,因此尝试下面的一个没有意义,但我仍然尝试并得到了错误 org.json.JSONException: JSONObject["User"] not found.
System.out.println(jsonObj.getJSONArray("User").length());
(3) IF I try 'Users' ie: jsonObj.getJSONArray("Users").length()
, it says JSONObject["Users"] is not a JSONArray
.
(3) 如果我尝试 'Users' ie: jsonObj.getJSONArray("Users").length()
,它会说JSONObject["Users"] is not a JSONArray
.
(4)
(4)
JSONObject jsonObj = service.getMyData();
JSONObject jUsers = jsonObj.getJSONObject("Users");
JSONObject jUser = jUsers.getJSONObject("User");
System.out.println(jUser.length());
Error: org.json.JSONException: JSONObject["User"] is not a JSONObject.
错误: org.json.JSONException: JSONObject["User"] is not a JSONObject.
XML:
XML:
<Users>
<User>
<Name>Unni</Name>
<Books>
<Book>book1</Book>
<Book>book2</Book>
<Book>book3</Book>
</Books>
</User>
<User>
<Name>Ammu</Name>
<Books>
<Book>book1</Book>
<Book>book2</Book>
<Book>book4</Book>
</Books>
</User>
</Users>
JSON:
JSON:
{
"Users": {
"User": [
{
"Name": "Unni",
"Books": {
"Book": [
"book1",
"book2",
"book3"
]
}
},
{
"Name": "Ammu",
"Books": {
"Book": [
"book1",
"book2",
"book4"
]
}
}
]
}
}
采纳答案by Wundwin Born
In JSON
format,
在JSON
格式上,
[]
is JSONArray
[]
是 JSONArray
{}
is JSONObject
{}
是 JSONObject
In your post,
在你的帖子里,
- Users- JSONObject
- User- JSONArray
- Books- JSONObject
- Book- JSONArray
- 用户- JSONObject
- 用户- JSONArray
- 书籍- JSONObject
- 书- JSONArray
Error: org.json.JSONException: JSONObject["User"] is not a JSONObject.
As mentioned above, try with getJSONArray()
如上所述,尝试使用 getJSONArray()
JSONArray userArray = jUsers.getJSONArray("User");
回答by spiderman
I had to call the getJSONArray
method when retrieving the array.
我getJSONArray
在检索数组时必须调用该方法。
JSONObject jsonObj = service.getMyData();
JSONObject jUsers = jsonObj.getJSONObject("Users");
System.out.println(jUsers.getJSONArray("User").length());