gpt4 book ai didi

SwiftyJson 不从 NSBundle 解析

转载 作者:行者123 更新时间:2023-11-30 13:49:29 37 4
gpt4 key购买 nike

我的存储中的文件中有一个大的 Json 数据,我正在尝试使用 SwiftyJson 解析该 json 。这是我的代码:

  let path  = NSBundle.mainBundle().pathForResource("ostan_city", ofType: "JSON")!
do {
let jsonDta = try NSData(contentsOfURL: NSURL(fileURLWithPath: path), options: NSDataReadingOptions.DataReadingMappedIfSafe)
let json = JSON(data: jsonDta)
for item in json["ostan"].arrayValue {
var citiesArray = [city]()
for cities in item["cities"].arrayValue {
citiesArray.append(city(id: cities["id"].stringValue , name: cities["name"].stringValue ))
}
ostans.append(ostan(id: item["id"].stringValue, name: item["name"].stringValue , cities: citiesArray) )
citiesArray = []
}

}catch {

}

我已经调试过它,路径和 jsonDta 都有值,但在 JSON 方法运行后,它内部没有值,并且 swiftyJson 不返回任何内容。

有什么问题吗?这是从 nsbundle 获取 json 的正确方法吗?

谢谢

最佳答案

你的代码没有问题。问题出在您的 ostan_city.JSON 中,它不是 JSON 格式。

关于SwiftyJson 不从 NSBundle 解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34469223/

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