Get multiple strings from JSON

136 Views Asked by At

So the JSON variable let json = JSON(nearbyChargingSites.jsonString!) contains the current data.:

{
  "timestamp" : 1626902257093,
  "superchargers" : [
    {
      "location" : {
        "lat" : 63.325319,
        "long" : 10.305137
      },
      "total_stalls" : 19,
      "distance_miles" : 10.064082000000001,
      "type" : "supercharger",
      "site_closed" : false,
      "available_stalls" : 15,
      "name" : "Leinstrand, Norway - Klett"
    },
    {
      "location" : {
        "lat" : 63.466445999999998,
        "long" : 10.91766
      },
      "total_stalls" : 16,
      "distance_miles" : 11.838984999999999,
      "type" : "supercharger",
      "site_closed" : false,
      "available_stalls" : 16,
      "name" : "Stjørdal, Norway"
    },
    {
      "location" : {
        "lat" : 63.734355000000001,
        "long" : 11.281487
      },
      "total_stalls" : 12,
      "distance_miles" : 31.206503999999999,
      "type" : "supercharger",
      "site_closed" : false,
      "available_stalls" : 11,
      "name" : "Levanger, Norway"
    },
    {
      "location" : {
        "lat" : 62.832030000000003,
        "long" : 10.009639999999999
      },
      "total_stalls" : 20,
      "distance_miles" : 44.117753,
      "type" : "supercharger",
      "site_closed" : false,
      "available_stalls" : 17,
      "name" : "Berkåk, Norway"
    }
  ],
  "congestion_sync_time_utc_secs" : 1626902199,
  "destination_charging" : [
    {
      "distance_miles" : 23.366278999999999,
      "name" : "Bårdshaug Herregård",
      "location" : {
        "lat" : 63.299208,
        "long" : 9.8448650000000004
      },
      "type" : "destination"
    },
    {
      "distance_miles" : 38.390034,
      "name" : "Fosen Fjordhotel",
      "location" : {
        "lat" : 63.959356999999997,
        "long" : 10.223908
      },
      "type" : "destination"
    },
    {
      "distance_miles" : 46.220022999999998,
      "name" : "Falksenteret",
      "location" : {
        "lat" : 63.293301999999997,
        "long" : 9.0834460000000004
      },
      "type" : "destination"
    },
    {
      "distance_miles" : 54.981445000000001,
      "name" : "Væktarstua",
      "location" : {
        "lat" : 62.908683000000003,
        "long" : 11.893306000000001
      },
      "type" : "destination"
    }
  ]
}

I use SwiftyJSON and tries to get the superchargers latitude, longitude and name, like this:

let jsonName = json["superchargers"]["name"]
let jsonLat = json["superchargers"]["location"]["lat"]
let jsonLong = json["superchargers"]["location"]["long"]
            

When trying to print any of those, all of them return nil.

Any ideas what I am doing wrong, and how to do this?

The reason I want to do this is because I want to add them as annotation to a MKMapView.

1

There are 1 best solutions below

12
Christos Koninis On

The first error is that the JSON initializer you are using will create a single JSON string object, it will not parse the string as JSON data:

Instead of:

let json = JSON(nearbyChargingSites.jsonString!)

you need to use:

let json = JSON(data: dataFromJSONString)

Second you need to iterate over the superchargers array to collect all the values

Try something like:

    if let dataFromString = nearbyChargingSites.jsonString!.data(using: .utf8, allowLossyConversion: false) {
        let json = try! JSON(data: dataFromString,options: .allowFragments)

        for supercharger in json["superchargers"].arrayValue {
            let jsonName = supercharger["name"].stringValue
            let jsonLat = supercharger["location"]["lat"].doubleValue
            let jsonLong = supercharger["location"]["long"].doubleValue
        }
    }

Please note that the above code does not perform error handling and will crash if values are missing from JSON.