/ / Problem z dekodowaniem JSON w Swift (brak danych) - json, swift, rest, swift4

Problem z dekodowaniem JSON w Swift (brak danych) - json, swift, rest, swift4

Próbuję zapytać API obrazu NASA (najnowsze dokumenty tutaj) za pomocą Swift 4. Skonfigurowałem i przetestowałem moje żądanie za pomocą JSONPlaceholder aby upewnić się, że moje żądanie sieciowe i dekodowanie byłypoprawnie skonfigurowane. Wszystko działało dobrze, ale kiedy zmieniłem adres URL i odpowiednią strukturę danych JSON, pojawia się błąd informujący, że „danych nie można odczytać, ponieważ ich brakuje”.

Użyłem Listonosza do sprawdzenia, czy JSON jest zwracany, i do zbudowania struktury danych JSON.

Czy jest to częsty błąd podczas dekodowania JSON, czy jest to coś z żądaniem sieci? A może brakuje mi czegoś z użyciem API NASA?

let NASAURL = URL(string: "https://images-api.nasa.gov/search?q=moon")
let session = URLSession(configuration: .default)
let task = session.dataTask(with: NASAURL!) { (rdata, response, error) in
NSLog("Data Description: " + (rdata!.debugDescription) + "nResponse: " + response.debugDescription + "nError Description: " + error.debugDescription)

guard rdata != nil else{
NSLog("No data")
return
}
guard error == nil else{
NSLog(response.debugDescription + "n")
NSLog(error.debugDescription)
NSLog(error.debugDescription)
return
}
let decoder = JSONDecoder()
do{
NSLog(rdata.debugDescription)
let usr = try decoder.decode(Collect.self, from: rdata!) // Throws
NSLog(usr.href)

} catch {
NSLog("Error: " + error.localizedDescription)
}
}
task.resume()

// Collect is in its own class/file
struct Collect: Codable {
var href: String
//var items: [Items]
}

Poniżej znajduje się wydruk z powyższych instrukcji dziennika ...

2017-09-29 19:50:24.135288-0500 OpenNASA[16993:10774203] Data Description: 67669 bytes
Response: Optional(<NSHTTPURLResponse: 0x60000003db00> { URL: https://images-api.nasa.gov/search?q=moon } { status code: 200, headers {
"Access-Control-Allow-Origin" = "*";
"Cache-Control" = "public, max-age=300, s-maxage=600";
"Content-Encoding" = gzip;
"Content-Length" = 9334;
"Content-Type" = "application/json; charset=utf-8";
Date = "Sat, 30 Sep 2017 00:48:11 GMT";
Server = "nginx/1.4.6 (Ubuntu)";
"Strict-Transport-Security" = "max-age=31536000";
Vary = "Accept-Encoding";
"access-control-allow-headers" = "Origin,Content-Type,Accept,Authorization,X-Requested-With";
"access-control-allow-methods" = GET;
} })
Error Description: nil
2017-09-29 19:50:24.137324-0500 OpenNASA[16993:10774203] Optional(67669 bytes)
2017-09-29 19:56:01.843750-0500 OpenNASA[16993:10774203] Error: The data couldn’t be read because it is missing.

Odpowiedzi:

1 dla odpowiedzi № 1

Twój kod powinien być jak poniżej:

struct Collect: Codable {
var collection: Links
}
struct Links: Codable {
var links: [Href]
}

struct Href: Codable {
var href: String
}

Musisz zadzwonić jak poniżej:

let usr = try decoder.decode(Collect.self, from: rdata!) // Throws
let links = usr.collection.links
for link in links {
print(link.href)
}