0

I'm looking at this API for covid19 on RapidAPI. I Tested the endpoint and it showed this result:

[0:
"country":"Canada"
"provinces":[

    0:{
    "province":"Alberta"
    "confirmed":754
    "recovered":0
    "deaths":9
    "active":0
    }...etc]

"latitude":56.130366
"longitude":-106.346771
"date":"2020-04-01"
}
]

Pretty straight forward. I want to parse the "provinces" segment, so in xcode I set up a couple models like this:

struct Country: Codable{
let country: String
let provinces: [Province]
}

struct Province: Codable{
let province: String
let confirmed: Int
let recovered: Int
let deaths: Int
let active: Int
}

I believe this is correct, but it won't parse. It only works when I comment out this bit:

struct Country: Codable{
let country: String
//let provinces: [Province]
}

meaning I can print out the name of the country, but only when the provinces object is commented out. This makes me think that there is something wrong with my model. What am I doing wrong here? I've looked up other examples and this should be working... I'm pretty sure.

EDIT: I'll add a bit more code to make it clearer what I'm doing:

override func viewDidLoad() {
    super.viewDidLoad()

    Service.shared.getInfo(requestURL: "url", host: "host", key: "12345", needsKey: true) { data in
        
        if let data = data{
            if let p = try? JSONDecoder().decode([Country].self, from: data){
                //get the data and set it to a string
                let provinceName: String = p[0].provinces[0].province
                self.provinceStr = provinceName
            }
            
            DispatchQueue.main.async {
                [unowned self] in
                //print the string
                print(self.provinceStr)
            }
        }
        
    }
}
  • 2
    the data you get from your API (the data that you show) is not JSON, it does not even have commas separating the elements. Trying to JSON decode data that is not JSON is not possible. – workingdog support Ukraine Sep 05 '21 at 00:44
  • Also, what error does the JSON decoding give you? Print out the `error` from the catch statement (be careful not to use the `localizedDescription` otherwise you won't get the full error message). – George Sep 05 '21 at 00:47
  • "Pretty straight forward. I want to parse the "provinces" segment" What does that mean? Nobody but you knows whether you are using `JSONDecoder`, `JSONSerialization` or whatever. – El Tomato Sep 05 '21 at 01:16
  • Okay I added some more context to my main post of what I'm trying to do. Sorry if i'm being unclear, I'm still new to all this. Like I said, if i comment out "let provinces: [Province]" in my struct, then try to get "let provinceName: String = p[0].country" from the data, it prints no problem, so I know that i'm able to parse the data from the JSON. Theres got to be something wrong with my model. –  Sep 05 '21 at 02:10
  • Oh, George, sorry I forgot to answer you. I'm not getting any errors, just a successful 200 response, but I'm not getting a print out of the name of the province. If you look at the code above that I added, the console should be printing "Alberta" –  Sep 05 '21 at 02:14
  • could you add `print("--> data: " + String(data: data, encoding: .utf8))` in your `viewDidLoad` withing `Service.shared.getInfo` and show us exactly what it prints – workingdog support Ukraine Sep 05 '21 at 03:29
  • Sure. Here was the output --> data: [{"country":"Canada","code":"CA","confirmed":1513189,"recovered":1450499,"critical":405,"deaths":27016,"latitude":56.130366,"longitude":-106.346771,"lastChange":"2021-09-04T22:25:03+02:00","lastUpdate":"2021-09-05T05:30:03+02:00"}] So the province name isn't even in the data. Hmm. BTW thanks for teaching me how to print the data. –  Sep 05 '21 at 03:48
  • That is a very different json message, are you sure this is from the same endpoint? – Joakim Danielson Sep 05 '21 at 10:42
  • Yeah I just double checked. This is the rapidAPI link if you want to take a look: https://rapidapi.com/Gramzivi/api/covid-19-data/ I am using "getDailyReportByCountryCode" –  Sep 05 '21 at 14:39

1 Answers1

0

from the printout, this is the data structure you should be using:

struct Country: Codable {
    let country, code: String
    let confirmed, recovered, critical, deaths: Int
    let latitude, longitude: Double
    let lastChange, lastUpdate: Date  // <-- or String
}
  • Thank you! I figured that out with your suggestion of printing the data. I'm just confused now about why the data differed from the results that rapidAPI showed me when I tested the endpoint (I have a feeling theres something I'm not understanding about how the site works). At any rate, I can easily just print the data from now on to see what I need to parse from rapidAPI. –  Sep 05 '21 at 14:36
  • you may want to look at this SO question, where the OP had his App rejected because it mentioned covid-19. https://stackoverflow.com/questions/68996858/remove-specify-items-from-array-when-making-a-network-request – workingdog support Ukraine Sep 05 '21 at 22:25
  • Oh thank you. I'm just doing this for practice with JSON, in fact that API i'm using doesn't have up to date info. I completed the app thanks to your help, btw! –  Sep 06 '21 at 16:32