Json Array

Hey everyone,


I am currently facing a problem which I can't solve. Hope somebody can help me, because I haven't found anything that worked for me. 🙂

The body of the response looks like that:

[

{

"position1" : "someData",

"position2" : "someData",

"position3" : "someData",

"position4" : "someData",

"position5" : "someData",

"position6" : "someData",

"position7" : "someData",

"sparkline_in_7d": {

"price": [

65757.63,

54367.63,

45346.63,

75858.63,

89894.63,

78748.63,

76846.63

]

}

}

]

I want to get the data from the price Array, and store it in Array with which I am drawing a chart.

Hope somebody can help me. 🙂

Replies

Could you illustrate better what you want to get from those data.

Is that proper JSON? I would have expected it to have some identifier for the outermost array and then enclosed in {}. When I add that, the following code works:


let theJSON = """
     {"records": [
          {
               "position1" : "someData",
               "position2" : "someData",
               "position3" : "someData",
               "position4" : "someData",
               "position5" : "someData",
               "position6" : "someData",
               "position7" : "someData",
               "sparkline_in_7d": {
                    "price": [
                         65757.63,
                         54367.63,
                         45346.63,
                         75858.63,
                         89894.63,
                         78748.63,
                         76846.63
                    ]
               }
          }
     ]}
"""

struct Record : Codable, Equatable {
    var position1 : String
    var position2 : String
    var position3 : String
    var position4 : String
    var position5 : String
    var position6 : String
    var position7 : String
    var sparkline_in_7d : [String : [Double]]
}

struct Response : Codable, Equatable {
    var records : [Record]
}

if let theData = theJSON.data(using: .utf8) {
    if let theResponse = try? JSONDecoder().decode(Response.self, from: theData) {
        for theRecord in theResponse.records {
            if let thePriceArray = theRecord.sparkline_in_7d["price"] {
                print(thePriceArray)     // prints out [65757.63, 54367.53, ...
            } else {
                print("empty dictionary")
            }
        }
    } else {
        print("could not decode")
    }
} else {
    print("could not create data")
}

IIRC it is valid for JSON to be an array at the topmost level. In that case, your decoding code would look something like this:


    if let records = try? JSONDecoder().decode([Record].self, from: theData) {
        for theRecord in records {  
           …

If it helps, tutorial on parsing JSON with Swift 4....

https://www.youtube.com/watch?v=XqNvHHAi08s&t=34s

The Json looks like I said above, I get it from an public api.

Thats the api link: https://api.coingecko.com/api/v3/coins/markets?vs_currency=eur&ids=bitcoin&sparkline=true

I have already tried several thing, but for some reason I don't get it to work.

Maybe the link declares my situation better 🙂.