gpt4 book ai didi

arrays - 在 Swift 中解析 json 数组

转载 作者:行者123 更新时间:2023-11-28 13:37:32 26 4
gpt4 key购买 nike

我正在尝试获取一些 json 数据,如下所示:

[
{
"_id": "5ccbf88042b2f60ec690a8dd",
"title": "Conference1",
"description": "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.",
"cities": [
{
"name": "Paris",
"numberOfUsers": "3"
},
{
"name": "Marseille",
"numberOfUsers": "7"
},
{
"name": "Lyon",
"numberOfUsers": "2"
}
]
}

{
"_id": "5ccbf88042b2f60ec690a8dd",
"title": "Conference1",
"description": "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.",
"cities": [
{
"name": "Paris",
"numberOfUsers": "5"
},
{
"name": "Marseille",
"numberOfUsers": "10"
},
{
"name": "Lyon",
"numberOfUsers": "8"
}
]
}

]

这是我的代码:

class Event: NSObject{

var title: String? = ""
var eventDescription: String? = ""
var cities: [String:String]? = ["":""]
var name: String? = ""
var numberOfUsers: String? = ""


static func parseEventData(data: Data) -> [Event] {
var eventsArray = [Event]()

do {
let jsonResult = try JSONSerialization.jsonObject(with: data, options: .mutableContainers)

//Parse JSON Data
if let events = jsonResult as? [Dictionary<String,AnyObject>] {
for event in events {
let newEvent = Event()
newEvent.title = event["title"] as? String
newEvent.eventDescription = event["description"] as? String

newEvent.cities = event["cities"] as? [String:String]
for city in newEvent.cities? {
newEvent.name = city["name"] as? String
newEvent.numberOfUsers = city["numberOfUsers"] as? String
}



eventsArray.append(newEvent)
}
}

}catch let err {
print(err)
}

return eventsArray
}
}

代码可以很好地编译标题和描述,但我无法正确捕捉城市。任何帮助,将不胜感激。谢谢

最佳答案

更正 json(你错过了数组元素 , 之间的逗号)

[{
"_id": "5ccbf88042b2f60ec690a8dd",
"title": "Conference1",
"description": "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.",
"cities": [{
"name": "Paris",
"numberOfUsers": "3"
},
{
"name": "Marseille",
"numberOfUsers": "7"
},
{
"name": "Lyon",
"numberOfUsers": "2"
}
]
},
{
"_id": "5ccbf88042b2f60ec690a8dd",
"title": "Conference1",
"description": "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.",
"cities": [{
"name": "Paris",
"numberOfUsers": "5"
},
{
"name": "Marseille",
"numberOfUsers": "10"
},
{
"name": "Lyon",
"numberOfUsers": "8"
}
]
}

]

`// MARK: - Element
struct Root: Codable {
let id, title, purpleDescription: String
let cities: [City]

enum CodingKeys: String, CodingKey {
case id = "_id"
case title
case purpleDescription = "description"
case cities
}
}

// MARK: - City
struct City: Codable {
let name, numberOfUsers: String
}

let res = try! JSONDecoder().decode([Root].self,from:data)
print(res)

编辑:这里是(城市是一个数组)

 newEvent.cities = event["cities"] as? [String:String]

应该是

 newEvent.cities = event["cities"] as? [[String:String]]

do {
let jsonResult = try JSONSerialization.jsonObject(with: data, options:[])

//Parse JSON Data
if let events = jsonResult as? [[String:Any]] {
for event in events {
let newEvent = Event()
newEvent.title = event["title"] as? String
newEvent.eventDescription = event["description"] as? String

newEvent.cities = event["cities"] as? [[String:String]]
for city in newEvent.cities ?? [["no city found": "number of users : 0"]] {
newEvent.name = city["name"] ?? ""
newEvent.numberOfUsers = city["numberOfUsers"] ?? ""
}
eventsArray.append(newEvent)
}
}

}catch {
print(error)
}

关于arrays - 在 Swift 中解析 json 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56430387/

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