gpt4 book ai didi

android - 嵌套的 JSON 对象长度

转载 作者:行者123 更新时间:2023-11-30 03:59:20 27 4
gpt4 key购买 nike

我有以下 JSON 字符串。

{
"data": [
{
"city_id": "1",
"city_name_eng": "Multan",
"0": {
"category_id": "1",
"city_id": "1",
"category_name_eng": "Mango",
"0": {
"product_id": "1",
"category_id": "1",
"product_name_eng": "Mango1"
},
"1": {
"product_id": "2",
"category_id": "1",
"product_name_eng": "Mango2"
},
"2": {
"product_id": "3",
"category_id": "1",
"product_name_eng": "Mango3"
},
"3": {
"product_id": "4",
"category_id": "1",
"product_name_eng": "Mango1"
}
},
"1": {
"category_id": "2",
"city_id": "1",
"category_name_eng": "Shoes"
},
"2": {
"category_id": "3",
"city_id": "1",
"category_name_eng": "Bank"
}
},
{
"city_id": "2",
"city_name_eng": "Lahore",
"3": {
"category_id": "4",
"city_id": "2",
"category_name_eng": "Food"
},
"4": {
"category_id": "5",
"city_id": "2",
"category_name_eng": "Computer"
},
"5": {
"category_id": "6",
"city_id": "2",
"category_name_eng": "Mobile"
}
}
]
}

我想要 JSONObject 的长度。基本上,具有 "city_id": "1" 的第一个对象有 4 个嵌套的 JSONObjects 和 2 个值,即 city_id 和 city_name_eng。当我使用 objJSON.length() 时,它给了我 6 的长度。但相反,我想有 4 个嵌套的 JSONObjects,我怎样才能在运行时得到它? (嵌套的jsonobjects是3个还是4个还是任意数字)

如何区分嵌套的 JSONObject 和键值?

最佳答案

如果网站是您自己的,那么我建议修改代码以在适当的地方返回 JSONArray

否则,jsonObj.keys() 将返回一个Iterator,您可以使用它来迭代键并解析出您的对象。

关于android - 嵌套的 JSON 对象长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12777496/

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