1

I want to get the value of City: in my json structure. I receive the data inside a Dictionary with dictionary: [String : Any]?

I already tried several methods but nothing worked for me. I think my problem is because auf the nested structure with the multiple { and [

{
"Format": "XXXXXXXXX",
"FormatVersion": "1.0",
"Status": "OK",
"NumToReturn": 1,
"AllResults": [
{
  "ConversationState": {
    "ConversationStateTime": XXXXXXXXX,
    "QueryEntities": {
      "Where": [
        {
          "Type": "City",
          "Label": "Stuttgart, Germany",
          "SpokenLabel": "Stuttgart",
          "Address": "Stuttgart, Baden-Wurttemberg, Germany",
          "City": "Stuttgart",
          "Admin2": "Regierungsbezirk Stuttgart",
          "Admin1": "Baden-Wurttemberg",
          "Country": "Germany",
          "CountryCode": "DE",
          "IATA": "STR",
          "Geohash": "u0wt8bd9854n",
          "Verified": true,
          "HighConfidence": true,
          "CurrentLocation": false,
          "Latitude": 48.78231811523438,
          "Longitude": 9.177020072937012,
          "ReferenceDatum": "WGS84",
          "TimeZone": "Europe/Berlin",
          "Radius": 10,
          "Links": [
            {
              "Label": "Wikipedia",
              "URL": "http://en.wikipedia.org/wiki/Stuttgart"
            }
          ],
          "TypeID": 5,
          "SourceID": 2,
          "RecordID": 2825297
        }
      ]
    }
   }
  }
 ]
}
1
  • post your code, please Commented Dec 24, 2018 at 8:51

3 Answers 3

1
import Foundation

struct YourClass: Codable {
    let format, formatVersion, status: String
    let numToReturn: Int
    let allResults: [AllResult]

    enum CodingKeys: String, CodingKey {
        case format = "Format"
        case formatVersion = "FormatVersion"
        case status = "Status"
        case numToReturn = "NumToReturn"
        case allResults = "AllResults"
    }
}

struct AllResult: Codable {
    let conversationState: ConversationState

    enum CodingKeys: String, CodingKey {
        case conversationState = "ConversationState"
    }
}

struct ConversationState: Codable {
    let conversationStateTime: String
    let queryEntities: QueryEntities

    enum CodingKeys: String, CodingKey {
        case conversationStateTime = "ConversationStateTime"
        case queryEntities = "QueryEntities"
    }
}

struct QueryEntities: Codable {
    let queryEntitiesWhere: [Where]

    enum CodingKeys: String, CodingKey {
        case queryEntitiesWhere = "Where"
    }
}

struct Where: Codable {
    let type, label, spokenLabel, address: String
    let city, admin2, admin1, country: String
    let countryCode, iata, geohash: String
    let verified, highConfidence, currentLocation: Bool
    let latitude, longitude: Double
    let referenceDatum, timeZone: String
    let radius: Int
    let links: [Link]
    let typeID, sourceID, recordID: Int

    enum CodingKeys: String, CodingKey {
        case type = "Type"
        case label = "Label"
        case spokenLabel = "SpokenLabel"
        case address = "Address"
        case city = "City"
        case admin2 = "Admin2"
        case admin1 = "Admin1"
        case country = "Country"
        case countryCode = "CountryCode"
        case iata = "IATA"
        case geohash = "Geohash"
        case verified = "Verified"
        case highConfidence = "HighConfidence"
        case currentLocation = "CurrentLocation"
        case latitude = "Latitude"
        case longitude = "Longitude"
        case referenceDatum = "ReferenceDatum"
        case timeZone = "TimeZone"
        case radius = "Radius"
        case links = "Links"
        case typeID = "TypeID"
        case sourceID = "SourceID"
        case recordID = "RecordID"
    }
}

struct Link: Codable {
    let label: String
    let url: String

    enum CodingKeys: String, CodingKey {
        case label = "Label"
        case url = "URL"
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

If your JSON is not example and you are using it, there is invalid JSON value in ConversationStateTime. Its value should be inside double quote unless it is an integer value.

"ConversationStateTime": "XXXXXXXXX"

Hope this help!

Comments

0
  1. Please check your JSON. "ConversationStateTime" doesn't have proper value
  2. If its valid, please try this code to get "City" value from your JSON

    // response is your JSON
    
     func parseJSON() {
        let allResults = response!["AllResults"] as? NSArray
    
        for (_, element) in (allResults?.enumerated())! {
            let elementDict = element as? Dictionary<String, AnyObject>
            let conversationStateDict = elementDict!["ConversationState"] as? Dictionary<String, AnyObject>
            let queryDict = conversationStateDict!["QueryEntities"] as? Dictionary<String, AnyObject>
            let whereDict = queryDict!["Where"] as? NSArray
    
            for (_, whereDictElement) in (whereDict?.enumerated())! {
                let dict = whereDictElement as? Dictionary<String, AnyObject>
                print(dict!["City"])
            }
        }
    }
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.