为什么我的 json 响应不符合我的结构?
Why is my json response not conforming to my struct?
我做了一个简单的 api 来测试 apiary,我已经为响应创建了一个结构。但是当调用时,由于某种原因我试图让它符合我的结构,我没有得到 body 而只得到 header。我想获得小联盟球队并将它们放入此结构中,然后打印它们。
APIlink:
https://littleleaguers.docs.apiary.io/#reference/0/little-league-teams/list-all-little-league-teams
回应Body:
{
"name": "Little League Teams"
"teams": [
{
"id": 1,
"team": "Yankees",
"players": [
{
"id": 1,
"position": 1,
"player": null
},
{
"id": 2,
"position": 2,
"player": {
"name": "John",
"last": "Johnson",
"jersey": 5
"signed_up": "2018-07-16T08:40:51.620Z"
}
},
{
"id": 3,
"position": 3,
"player": null
},
{
"id": 4,
"position": 4,
"player": null
},
{
"id": 5,
"position": 5,
"player": {
"name": "Blake",
"last": "Smith",
"jersey": 28
"signed_up": "2018-07-14T18:23:23.000Z"
}
},
{
"id": 6,
"position": 6,
"player": null
},
{
"id": 7,
"position": 7,
"player": null
},
{
"id": 8,
"position": 8,
"player": null
},
{
"id": 9,
"position": 9,
"player": null
},
{
"id": 10,
"position": 10,
"player": null
},
{
"id": 11,
"position": 11,
"player": null
},
{
"id": 12,
"position": 12,
"player": null
}
]
},
{
"id": 2,
"team": "Red Sox",
"players": [
{
"id": 13,
"position": 1,
"player": null
},
{
"id": 14,
"position": 2,
"player": null
},
{
"id": 15,
"position": 3,
"player": null
},
{
"id": 16,
"position": 4,
"player": null
},
{
"id": 17,
"position": 5,
"player": null
},
{
"id": 18,
"position": 6,
"player": null
},
{
"id": 19,
"position": 7,
"player": null
}
]
}
]
}
League.swift:
struct League: Decodable {
var name: String
var teams: [Teams]
}
Team.swift:
struct Team: Decodable {
var id: Int
var team: String
var players: [Players]
}
Players.swift:
struct Players: Decodable {
var id: Int
var position: Int
var player: player?
}
struct player: Decodable {
var name: String
var last: String
var jersey: Int
var signed_up: String
}
然后我有一个 class 来检索此数据:
GetData.swift:
class GetData{
func getLeague(){
let url = URL(string: "https://private-4df2e8-littleleaguers.apiary-mock.com/league")!
let request = URLRequest(url: url)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
if let response = response {
print("RESPONSE \(response)")
/* if let data = data, let body = String(data: data, encoding: .utf8) {
print("BODY \(body)")
// this line prints the body
}*/
if let data = data, let dataObtained = try? JSONDecoder().decode(League.self, from: data){
print("DATA \(dataObtained).....")// this line does not print
}
} else {
print(error ?? "Unknown error")
}
}
task.resume()
}
}
然后我在 ViewController.swift 中调用它:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let getData = GetData()
getData.getLeague()
}
控制台中唯一打印的是:
RESPONSE <NSHTTPURLResponse: 0x6000008e5e80> { URL: https://private-4df2e8-littleleaguers.apiary-mock.com/league } { Status Code: 200, Headers {
"Access-Control-Allow-Origin" = (
"*"
);
"Content-Encoding" = (
gzip
);
"Content-Length" = (
427
);
"Content-Type" = (
"application/json"
);
Date = (
"Fri, 06 Mar 2020 13:35:40 GMT"
);
Vary = (
"Accept-Encoding"
);
"access-control-allow-methods" = (
"OPTIONS,GET,HEAD,POST,PUT,DELETE,TRACE,CONNECT"
);
"access-control-max-age" = (
10
);
"x-apiary-ratelimit-limit" = (
120
);
"x-apiary-ratelimit-remaining" = (
119
);
"x-apiary-transaction-id" = (
5e6251ac6b105b008135d67a
);
} }
修复返回的 JSON,这是错误的,结构模型中的所有内容都很好,请在 online swift playground:
上查看
import Foundation
let json = """
{
"name": "Little League Teams",
"teams": [
{
"id": 1,
"team": "Yankees",
"players": [
{
"id": 1,
"position": 1,
"player": null
},
{
"id": 2,
"position": 2,
"player": {
"name": "John",
"last": "Johnson",
"jersey": 5,
"signed_up": "2018-07-16T08:40:51.620Z"
}
},
{
"id": 3,
"position": 3,
"player": null
},
{
"id": 4,
"position": 4,
"player": null
},
{
"id": 5,
"position": 5,
"player": {
"name": "Blake",
"last": "Smith",
"jersey": 28,
"signed_up": "2018-07-14T18:23:23.000Z"
}
},
{
"id": 6,
"position": 6,
"player": null
},
{
"id": 7,
"position": 7,
"player": null
},
{
"id": 8,
"position": 8,
"player": null
},
{
"id": 9,
"position": 9,
"player": null
},
{
"id": 10,
"position": 10,
"player": null
},
{
"id": 11,
"position": 11,
"player": null
},
{
"id": 12,
"position": 12,
"player": null
}
]
},
{
"id": 2,
"team": "Red Sox",
"players": [
{
"id": 13,
"position": 1,
"player": null
},
{
"id": 14,
"position": 2,
"player": null
},
{
"id": 15,
"position": 3,
"player": null
},
{
"id": 16,
"position": 4,
"player": null
},
{
"id": 17,
"position": 5,
"player": null
},
{
"id": 18,
"position": 6,
"player": null
},
{
"id": 19,
"position": 7,
"player": null
}
]
}
]
}
""".data(using: .utf8)!
struct player: Decodable {
var name: String
var last: String
var jersey: Int
var signed_up: String
}
struct Players: Decodable {
var id: Int
var position: Int
var player: player?
}
struct Team: Decodable {
var id: Int
var team: String
var players: [Players]
}
struct League: Decodable {
var name: String
var teams: [Team]
}
if let results = try? JSONDecoder().decode(League.self, from: json) {
print(results)
}
输出:
League(name: "Little League Teams", teams: [SwiftPlayground.Team(id:
1, team: "Yankees", players: [SwiftPlayground.Players(id: 1, position:
1, player: nil), SwiftPlayground.Players(id: 2, position: 2, player:
Optional(SwiftPlayground.player(name: "John", last: "Johnson", jersey:
5, signed_up: "2018-07-16T08:40:51.620Z"))),
SwiftPlayground.Players(id: 3, position: 3, player: nil),
SwiftPlayground.Players(id: 4, position: 4, player: nil),
SwiftPlayground.Players(id: 5, position: 5, player:
Optional(SwiftPlayground.player(name: "Blake", last: "Smith", jersey:
28, signed_up: "2018-07-14T18:23:23.000Z"))),
SwiftPlayground.Players(id: 6, position: 6, player: nil),
SwiftPlayground.Players(id: 7, position: 7, player: nil),
SwiftPlayground.Players(id: 8, position: 8, player: nil),
SwiftPlayground.Players(id: 9, position: 9, player: nil),
SwiftPlayground.Players(id: 10, position: 10, player: nil),
SwiftPlayground.Players(id: 11, position: 11, player: nil),
SwiftPlayground.Players(id: 12, position: 12, player: nil)]),
SwiftPlayground.Team(id: 2, team: "Red Sox", players:
[SwiftPlayground.Players(id: 13, position: 1, player: nil),
SwiftPlayground.Players(id: 14, position: 2, player: nil),
SwiftPlayground.Players(id: 15, position: 3, player: nil),
SwiftPlayground.Players(id: 16, position: 4, player: nil),
SwiftPlayground.Players(id: 17, position: 5, player: nil),
SwiftPlayground.Players(id: 18, position: 6, player: nil),
SwiftPlayground.Players(id: 19, position: 7, player: nil)])])
我做了一个简单的 api 来测试 apiary,我已经为响应创建了一个结构。但是当调用时,由于某种原因我试图让它符合我的结构,我没有得到 body 而只得到 header。我想获得小联盟球队并将它们放入此结构中,然后打印它们。
APIlink: https://littleleaguers.docs.apiary.io/#reference/0/little-league-teams/list-all-little-league-teams
回应Body:
{
"name": "Little League Teams"
"teams": [
{
"id": 1,
"team": "Yankees",
"players": [
{
"id": 1,
"position": 1,
"player": null
},
{
"id": 2,
"position": 2,
"player": {
"name": "John",
"last": "Johnson",
"jersey": 5
"signed_up": "2018-07-16T08:40:51.620Z"
}
},
{
"id": 3,
"position": 3,
"player": null
},
{
"id": 4,
"position": 4,
"player": null
},
{
"id": 5,
"position": 5,
"player": {
"name": "Blake",
"last": "Smith",
"jersey": 28
"signed_up": "2018-07-14T18:23:23.000Z"
}
},
{
"id": 6,
"position": 6,
"player": null
},
{
"id": 7,
"position": 7,
"player": null
},
{
"id": 8,
"position": 8,
"player": null
},
{
"id": 9,
"position": 9,
"player": null
},
{
"id": 10,
"position": 10,
"player": null
},
{
"id": 11,
"position": 11,
"player": null
},
{
"id": 12,
"position": 12,
"player": null
}
]
},
{
"id": 2,
"team": "Red Sox",
"players": [
{
"id": 13,
"position": 1,
"player": null
},
{
"id": 14,
"position": 2,
"player": null
},
{
"id": 15,
"position": 3,
"player": null
},
{
"id": 16,
"position": 4,
"player": null
},
{
"id": 17,
"position": 5,
"player": null
},
{
"id": 18,
"position": 6,
"player": null
},
{
"id": 19,
"position": 7,
"player": null
}
]
}
]
}
League.swift:
struct League: Decodable {
var name: String
var teams: [Teams]
}
Team.swift:
struct Team: Decodable {
var id: Int
var team: String
var players: [Players]
}
Players.swift:
struct Players: Decodable {
var id: Int
var position: Int
var player: player?
}
struct player: Decodable {
var name: String
var last: String
var jersey: Int
var signed_up: String
}
然后我有一个 class 来检索此数据:
GetData.swift:
class GetData{
func getLeague(){
let url = URL(string: "https://private-4df2e8-littleleaguers.apiary-mock.com/league")!
let request = URLRequest(url: url)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
if let response = response {
print("RESPONSE \(response)")
/* if let data = data, let body = String(data: data, encoding: .utf8) {
print("BODY \(body)")
// this line prints the body
}*/
if let data = data, let dataObtained = try? JSONDecoder().decode(League.self, from: data){
print("DATA \(dataObtained).....")// this line does not print
}
} else {
print(error ?? "Unknown error")
}
}
task.resume()
}
}
然后我在 ViewController.swift 中调用它:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let getData = GetData()
getData.getLeague()
}
控制台中唯一打印的是:
RESPONSE <NSHTTPURLResponse: 0x6000008e5e80> { URL: https://private-4df2e8-littleleaguers.apiary-mock.com/league } { Status Code: 200, Headers {
"Access-Control-Allow-Origin" = (
"*"
);
"Content-Encoding" = (
gzip
);
"Content-Length" = (
427
);
"Content-Type" = (
"application/json"
);
Date = (
"Fri, 06 Mar 2020 13:35:40 GMT"
);
Vary = (
"Accept-Encoding"
);
"access-control-allow-methods" = (
"OPTIONS,GET,HEAD,POST,PUT,DELETE,TRACE,CONNECT"
);
"access-control-max-age" = (
10
);
"x-apiary-ratelimit-limit" = (
120
);
"x-apiary-ratelimit-remaining" = (
119
);
"x-apiary-transaction-id" = (
5e6251ac6b105b008135d67a
);
} }
修复返回的 JSON,这是错误的,结构模型中的所有内容都很好,请在 online swift playground:
上查看import Foundation
let json = """
{
"name": "Little League Teams",
"teams": [
{
"id": 1,
"team": "Yankees",
"players": [
{
"id": 1,
"position": 1,
"player": null
},
{
"id": 2,
"position": 2,
"player": {
"name": "John",
"last": "Johnson",
"jersey": 5,
"signed_up": "2018-07-16T08:40:51.620Z"
}
},
{
"id": 3,
"position": 3,
"player": null
},
{
"id": 4,
"position": 4,
"player": null
},
{
"id": 5,
"position": 5,
"player": {
"name": "Blake",
"last": "Smith",
"jersey": 28,
"signed_up": "2018-07-14T18:23:23.000Z"
}
},
{
"id": 6,
"position": 6,
"player": null
},
{
"id": 7,
"position": 7,
"player": null
},
{
"id": 8,
"position": 8,
"player": null
},
{
"id": 9,
"position": 9,
"player": null
},
{
"id": 10,
"position": 10,
"player": null
},
{
"id": 11,
"position": 11,
"player": null
},
{
"id": 12,
"position": 12,
"player": null
}
]
},
{
"id": 2,
"team": "Red Sox",
"players": [
{
"id": 13,
"position": 1,
"player": null
},
{
"id": 14,
"position": 2,
"player": null
},
{
"id": 15,
"position": 3,
"player": null
},
{
"id": 16,
"position": 4,
"player": null
},
{
"id": 17,
"position": 5,
"player": null
},
{
"id": 18,
"position": 6,
"player": null
},
{
"id": 19,
"position": 7,
"player": null
}
]
}
]
}
""".data(using: .utf8)!
struct player: Decodable {
var name: String
var last: String
var jersey: Int
var signed_up: String
}
struct Players: Decodable {
var id: Int
var position: Int
var player: player?
}
struct Team: Decodable {
var id: Int
var team: String
var players: [Players]
}
struct League: Decodable {
var name: String
var teams: [Team]
}
if let results = try? JSONDecoder().decode(League.self, from: json) {
print(results)
}
输出:
League(name: "Little League Teams", teams: [SwiftPlayground.Team(id: 1, team: "Yankees", players: [SwiftPlayground.Players(id: 1, position: 1, player: nil), SwiftPlayground.Players(id: 2, position: 2, player: Optional(SwiftPlayground.player(name: "John", last: "Johnson", jersey: 5, signed_up: "2018-07-16T08:40:51.620Z"))), SwiftPlayground.Players(id: 3, position: 3, player: nil), SwiftPlayground.Players(id: 4, position: 4, player: nil), SwiftPlayground.Players(id: 5, position: 5, player: Optional(SwiftPlayground.player(name: "Blake", last: "Smith", jersey: 28, signed_up: "2018-07-14T18:23:23.000Z"))), SwiftPlayground.Players(id: 6, position: 6, player: nil), SwiftPlayground.Players(id: 7, position: 7, player: nil), SwiftPlayground.Players(id: 8, position: 8, player: nil), SwiftPlayground.Players(id: 9, position: 9, player: nil), SwiftPlayground.Players(id: 10, position: 10, player: nil), SwiftPlayground.Players(id: 11, position: 11, player: nil), SwiftPlayground.Players(id: 12, position: 12, player: nil)]), SwiftPlayground.Team(id: 2, team: "Red Sox", players: [SwiftPlayground.Players(id: 13, position: 1, player: nil), SwiftPlayground.Players(id: 14, position: 2, player: nil), SwiftPlayground.Players(id: 15, position: 3, player: nil), SwiftPlayground.Players(id: 16, position: 4, player: nil), SwiftPlayground.Players(id: 17, position: 5, player: nil), SwiftPlayground.Players(id: 18, position: 6, player: nil), SwiftPlayground.Players(id: 19, position: 7, player: nil)])])