gpt4 book ai didi

json - 使用 SwiftyJSON 解析 json

转载 作者:行者123 更新时间:2023-11-30 13:53:17 25 4
gpt4 key购买 nike

我一直在尝试在项目内使用 SwiftyJSON 作为我的虚拟 JSON 文件。

但我只能得到“null”或“nil”,而不是 JSON 文件中的数据。我可能会在这里寻求一点帮助。

这是我得到的回复:0无效的1 的姓名:无品牌 1:空

这是虚拟 JSON 文件;

{
"Cars": [

{
"Name": "111",
"Make": "2000"
},

{
"Name": "222",
"Make": "2010"
}

]

}

这是我尝试解析的地方;

import UIKit

class ViewController: UIViewController {

var numberOfRows = 0

override func viewDidLoad() {
super.viewDidLoad()

parseJSON()

}

func parseJSON() {

let path : String = NSBundle.mainBundle().pathForResource("jsonfile", ofType: "json") as String!
let jsonDATA = NSData(contentsOfFile: path) as NSData!

let readableJSON = JSON(data: jsonDATA, options: NSJSONReadingOptions.MutableContainers, error: nil)

let allList = readableJSON["Cars"]

numberOfRows = readableJSON["Cars"].count

print("\(numberOfRows)")

print("\(allList)")

let name1 = readableJSON["Cars"][0]["Name"].string
let make1 = readableJSON["Cars",0,"Make"]

print("Name of 1 : \(name1)")
print("Make of 1: \(make1)")

}


}

最佳答案

希望这个解决方案能够帮助您。

func parseJson () {

if let path = NSBundle.mainBundle().pathForResource("jsonfile", ofType: "json")
{
if let jsonData = try? NSData(contentsOfFile: path, options: .DataReadingMappedIfSafe)
{
let originalJson = JSON(data: jsonData)
for (_, subJson) in originalJson["Cars"] {
print(subJson["Name"])
print(subJson["Make"])
}
}
}
}

关于json - 使用 SwiftyJSON 解析 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33986378/

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