我正在尝试使用swift循环嵌套JSON,但我能够检索未嵌套的记录。我无法检索嵌套记录。我也尝试过valueForKeyPath,但它不起作用。
这就是我现在所做的:
这是JSON:
[
{
"id":"001",
"firstName":"Fred",
"lastName":"Blogs",
"role":"Producer"
},
{
"teamName":"A",
"members":[
{
"id":"002",
"firstName":"Michael",
"lastName":"Right",
"role":"Cook"
},
{
"id":"003",
"firstName":"James",
"lastName":"Brown",
"role":"Bus Driver"
},
{
"id":"004",
"firstName":"Liam",
"lastName":"Nelson",
"role":"Actor"
},
{
"id":"005",
"firstName":"Chris",
"lastName":"Tucker",
"role":"Actor"
},
{
"id":"006",
"firstName":"Richard",
"lastName":"Burton",
"role":"Golfer"
},
{
"id":"007",
"firstName":"Matt",
"lastName":"Smith",
"role":"Care Taker"
}
]
},
{
"teamName":"B",
"members":[
{
"id":"008",
"firstName":"Mark",
"lastName":"Jones",
"role":"Script Writter"
},
{
"id":"009",
"firstName":"Hello",
"lastName":"World",
"role":"Tester"
}
]
}
]
这是Swift代码:
func getJSON() {
guard let url = URL(string: "http://www.somejson.json") else { return }
do {
let data = try Data(contentsOf: url)
let json = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as AnyObject
guard let array = json as?[[String: Any]] else { return }
var teams = [Team]()
var team: Team
var i = 0
for items in array {
team = Team()
if let id = items["id"] as? String {
team.id = id
print("id", team.id)
}
if let firstName = items["firstName"] as? String {
team.firstName = firstName
print("FirstName", team.firstName)
}
if let lastName = items["lastName"] as? String {
team.lastName = lastName
print("LastName", team.lastName)
}
if let role = items["role"] as? String {
team.role = role
print("Role", team.role)
}
if let profileImageURL = items["profileImageURL"] as? String {
team.profileImageURL = profileImageURL
print("ProfileImageURL", team.profileImageURL)
}
if let teamName = items["teamName"] as? String {
print("TeamName", teamName)
//if let id = json?.value(forKeyPath:"members.id") {
// print("Test", id)
//}
}
teams.append(team)
}
print("Count", teams.count)
} catch {
}
}
答案 0 :(得分:2)
使用Swift Codable
协议是可行的方法。甚至还有Custom components等在线工具可以帮助您从JSON生成Swift代码。
解析JSON的示例代码:
// To parse the JSON, add this file to your project and do:
//
// let teams = try? JSONDecoder().decode([Team].self, from: jsonData)
import Foundation
typealias Team = [TeamElement]
struct TeamElement: Codable {
let id, firstName, lastName, role: String?
let teamName: String?
let members: [Member]?
}
struct Member: Codable {
let id, firstName, lastName, role: String
}
答案 1 :(得分:0)
您忘了允许JSON结构包含包含团队成员的项目“成员”这一事实。
func getJSON() {
guard let url = URL(string: "http://www.somejson.json") else { return }
do {
let data = try Data(contentsOf: url)
let json = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as AnyObject
guard let array = json as?[[String: Any]] else { return }
var teams = [Team]()
for teamJson in array {
var team = Team()
if let members = teamJson["members"] as? [[String: Any]] {
team.teamName = teamJson["teamName"] as! String
for memberJson in members {
var member = Member()
if let id = memberJson["id"] as? String {
member.id = id
print("id", member.id)
}
if let firstName = memberJson["firstName"] as? String {
member.firstName = firstName
print("FirstName", member.firstName)
}
if let lastName = memberJson["lastName"] as? String {
member.lastName = lastName
print("LastName", member.lastName)
}
if let role = memberJson["role"] as? String {
member.role = role
print("Role", member.role)
}
team.members.append(member)
}
}
teams.append(team)
}
print("Count", teams.count)
} catch {
}
}
您还必须编写代码来处理不在团队中的异常值producer
,但我相信您可以自己解决这个问题。 :)