gpt4 book ai didi

ios - 使用 swift 从 JSON 检索数据时出现问题

转载 作者:行者123 更新时间:2023-11-28 09:09:45 25 4
gpt4 key购买 nike

我正在使用 swift 从 JSON 中检索数据。我是 JSON 的新手。我不知道如何检索这个嵌套值。我的 JSON 格式如下。

//JSON DATA
{
"data": [
{
"category_id": 1,
"category_name": "category1",
"category_icon": "",
"category_child": [
{
"sub_category_id": 11,
"sub_category_name": "subcategoryname1",
"sub_category_icon": ""
},
{
"sub_category_id": 12,
"sub_category_name": "subcategoryname2",
"sub_category_icon": ""
}
]
},
{
"category_id": 2,
"category_name": "category2",
"category_icon": "",
"category_child": [
{
"sub_category_id": 21,
"sub_category_name": "subcategoryname12",
"sub_category_icon": ""
},
{
"sub_category_id": 22,
"sub_category_name": "subcategoryname22",
"sub_category_icon": ""
}
]
}
]
}

我尝试了一些方法。但是,我没能在字典中找到合适的元素。我的代码如下:

ATTEPMT 1:
var dict: NSDictionary! = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary

for(var i = 0; i < (dict.valueForKey("data") as NSArray).count; i++)
{
self.categFromJson.addObject((dict.valueForKey("data") as NSArray) .objectAtIndex(i))

}

for(var i = 0; i < (categFromJson.valueForKey("category_child") as NSArray).count; i++)
{
self.sub_ct_FromArr.addObject((categFromJson.valueForKey("category_child") as NSArray) .objectAtIndex(i))

}

for(var i = 0; i < (sub_ct_FromArr.valueForKey("sub_category_name") as NSArray).count; i++)
{
self.sub_ct_name_arr.addObject((sub_ct_FromArr.valueForKey("sub_category_name") as NSArray) .objectAtIndex(i))

}


//ATTEMPT 2
var dict: NSDictionary! = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary

for(var i = 0; i < (dict.valueForKey("data") as NSArray).count; i++)
{
categFromJson.addObject((dict.valueForKey("data") as NSArray) .objectAtIndex(i))

sub_ct_FromArr.addObject((categFromJson.valueForKey("category_child") as NSArray) .objectAtIndex(i))

sub_ct_name_arr.addObject((sub_ct_FromArr.valueForKey("sub_category_name") as NSArray) .objectAtIndex(i))
}

但是,对于这两种尝试,最终输出都是错误的并且是相同的。 Bcos,编码如下。

//CODING
println(sub_ct_name_arr.objectAtIndex(0))

//Output for this Code
(subcategoryname1,subcategoryname2)
//REQUIRED Output
subcategoryname1

请多指教。我不知道那是什么问题。

最佳答案

首先,可视化您拥有的数据类型非常重要。

Your data types

然后可以这样获取根对象,后面是哪个对象是哪个类型:

let dict = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as! NSDictionary

let dataArray = dict["data"] as! [[String:AnyObject]] // The array of dictionaries

for object in dataArray {
let category_id = object["category_id"] as! Int
let category_name = object["category_name"] as! String
// etc
let category_child = object["category_child"] as! [[String:AnyObject]]
for child in category_child {
let sub_category_id = child["sub_category_id"] as! Int
let sub_category_name = child["sub_category_name"] as! String
println(sub_category_id)
println(sub_category_name)
}
}

您也可以考虑使用类似 SwiftyJSON 的库如果您有很多解码工作要做。

关于ios - 使用 swift 从 JSON 检索数据时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29649862/

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