Come fare la chiamata di API che utilizza una struttura con un numero nel file json

0

Domanda

Fornire ulteriori dettagli:

Sto cercando di accedere a un livello da un'API (link qui sotto) che ha nella sua struttura, un numero all'inizio di uno dei livelli (25th_percentile). Io sono in grado di creare una costante nel mio modello, perché non riesco a far partire il lasciate che il valore con un numero (cioè 25 in 25th_percentile). Come si può accedere ai dati al 25 ° percentile livello?

Il mio codice è il seguente. Quando uso enum come suggerito, ho un "non conforme a decodificabile protocollo di errore".

     //
 //  SchoolData.swift
 //
 //

 import Foundation

 struct Results: Decodable {
     let results: [SelectedSchool]
 }



 struct SelectedSchool: Decodable, Identifiable {
     let id: Int
     let fed_sch_cd: String
     let latest: LatestData

 }
 struct LatestData: Decodable {
     let school: SchoolDetails
     let admissions: AdmissionsDetails
 }

 struct SchoolDetails: Decodable {
     let name: String
     let school_url: String
 }

 struct AdmissionsDetails: Decodable {
     let admission_rate: AdmissionRateDetails
     let sat_scores: SATScores
 }


 struct SATScores: Decodable {
     let midpoint: SATSubjects
     let the25th_percentile: SATSubjects2

     enum CodingKeys: String, CodingKey {
         case the25th_percentile = "25th_percentile"
     }
 }




 struct SATSubjects: Decodable {
     let math: Int?
     let writing: Int?
     let critical_reading: Int?
 }


 struct SATSubjects2: Decodable {
     let math: Int?
     let writing: Int?
     let critical_reading: Int?
 }


 struct AdmissionRateDetails: Decodable {
     let overall: Float?
 }

 struct URLName {
     let partialURL = "https://api.data.gov/ed/collegescorecard/v1/schools?school.name="
     var searchTerm: String = ""
     let apiKey = "&api_key=myapikey"
 }

L'API convertito in JSON.

api json swiftui
2021-11-23 03:43:03
1

Migliore risposta

0

prova questo:

struct SATTwentyFive: Decodable {
    let the25th_percentile: SubjectData
    
    enum CodingKeys: String, CodingKey {
        case the25th_percentile = "25th_percentile"
    }
}

EDIT1:

Provare questo tipo di strutture leggere i tuoi dati in formato json (per me):

struct Results: Decodable {
    let results: [SelectedSchool]
}

struct SelectedSchool: Decodable, Identifiable {
    let id: Int
    let fed_sch_cd: String
    let latest: LatestData
}

struct LatestData: Decodable {
    let school: SchoolDetails
    let admissions: AdmissionsDetails
}

struct SchoolDetails: Decodable {
    let name: String
    let school_url: String
}

struct AdmissionsDetails: Decodable {
    let admission_rate: AdmissionRateDetails
    let sat_scores: SATScores
}

// ---- here ----
struct SATScores: Decodable {
    let midpoint: SATSubjects
    let the25th_percentile: SATSubjects

    enum CodingKeys: String, CodingKey {
        case the25th_percentile = "25th_percentile"
        case midpoint = "midpoint"
    }
}

struct SATSubjects: Decodable {
    let math: Int?
    let writing: Int?
    let critical_reading: Int?
}

struct AdmissionRateDetails: Decodable {
    let overall: Float?
}

e decodificare Results come:

try JSONDecoder().decode(Results.self, from: data)
2021-11-25 03:09:56

grazie workingdog - quando faccio come suggerito, ho un non conformi alle decodificabile errore di protocollo.
boxscorepress.com

L'approccio nella mia risposta con il enum CodingKeys opere. Ho aggiornato la mia risposta. P. S: non postare la vostra chiave segreta, rimuoverlo ora.
workingdog

workingdog - rimossa la chiave API e GRAZIE!!!! Ho provato 1000 cose, ma a quanto pare non sopra. davvero apprezzare il vostro tempo!!!
boxscorepress.com

contento che si sta lavorando. Se la mia risposta è stata di utilizzo, potrebbe segnare come corretto, per favore.
workingdog

Ho fatto segnare ma non ho abbastanza “punti reputazione” ad accettare il mio segno. Ha detto che ha notato in qualche modo. Grazie ancora.
boxscorepress.com

oh, non sapevo che non si può accettare una risposta quando si posta una domanda. Grazie comunque.
workingdog

In altre lingue

Questa pagina è in altre lingue

Русский
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................