5 Replies
      Latest reply on Dec 19, 2018 7:07 PM by CartoonSmart.com
      mojo_10 Level 1 Level 1 (0 points)

        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.
        • Re: Json Array
          Claude31 Level 8 Level 8 (6,145 points)

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

          • Re: Json Array
            rsharp Level 3 Level 3 (155 points)

            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")
            }
            
            
            • Re: Json Array
              CartoonSmart.com Level 3 Level 3 (270 points)

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

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