米利亚长度 JSONObject JSON
我读到了下面 XML 在 java 并转换B.
[
].
并添加元数据并返回 JSON.
问题
:
// 自定义方法和呼叫
// length is 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]
What I tried?
/1/
// prints 1
API says: length//
Get the number of keys stored in the 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
/3/ 如果我试试 'Users' , IE:
, 他会说
.
/4/
错误:
XML
:
JSON
:
JSONObject
[
org.json.JSONObject
].
并添加元数据并返回 JSON.
问题
:
JSONObject jsonObj = service.getMyData//;
// 自定义方法和呼叫
int count = jsonObj.length//;
// length is 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]
What I tried?
/1/
System.out.println/jsonObj.length///;
// prints 1
API says: length//
Get the number of keys stored in the 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.
System.out.println/jsonObj.getJSONArray/"User"/.length///;
/3/ 如果我试试 'Users' , IE:
jsonObj.getJSONArray/"Users"/.length//
, 他会说
JSONObject["Users"] is not a JSONArray
.
/4/
JSONObject jsonObj = service.getMyData//;
JSONObject jUsers = jsonObj.getJSONObject/"Users"/;
JSONObject jUser = jUsers.getJSONObject/"User"/;
System.out.println/jUser.length///;
错误:
org.json.JSONException: JSONObject["User"] is not a JSONObject.
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
:
{
"Users": {
"User": [
{
"Name": "Unni",
"Books": {
"Book": [
"book1",
"book2",
"book3"
]
}
},
{
"Name": "Ammu",
"Books": {
"Book": [
"book1",
"book2",
"book4"
]
}
}
]
}
}
没有找到相关结果
已邀请:
2 个回复
二哥
赞同来自:
,
[]
is JSONArray
is JSONObject
In your post,
Users
- JSONObject
User
- JSONArray
Books
- JSONObject
Book
- JSONArray
Error: org.json.JSONException: JSONObject["User"] is not a JSONObject.
As mentioned above, try with
裸奔
赞同来自:
提取数组时。