-1

After JSONSerialization decoded and as! cast to NSDictionary get data below.

{
    language = {
        A00001 = {
            chineseSimplified = CIMB;
            chineseTraditional = CIMB;
            english = CIMB;
            japanese = CIMB;
            korean = CIMB;
        };
    };
}

How to access into nested NSDictionary?

I'm able to get one layer data through data["language"], somehow I can't access multiple layers like:

data["language"]["A00001"]["english"]
data["language"]?["A00001"]?["english"]?
data["language"]!["A00001"]!["english"]!

Xcode returns this error:

Type 'Any' has no subscript members

Reference similar question: How to access deeply nested dictionaries in Swift Accessing Nested NSDictionary values in Swift 3.0 How do I manipulate nested dictionaries in Swift, e.g. JSON data?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Txuver Jhi Wei
  • 318
  • 2
  • 5
  • 17
  • please provide link to JSON file or URL... – BlackMirrorz Mar 13 '18 at 03:56
  • How about using latest API in Swift 4, Codable? – Rikesh Subedi Mar 13 '18 at 04:03
  • You should not be using `NSDictionary` in Swift. Use a Swift dictionary. There are countless examples showing the proper way to work with JSON in Swift. And please search on the error. The "Type 'Any' has no subscript members" error has been discussed here many times. – rmaddy Mar 13 '18 at 04:07

1 Answers1

0

Try casting it into [String: [String: [String: String]]], which is a nested structure for Swift Dictionary.

let json = """
{
"language": {
    "A00001": {
        "chineseSimplified" : "CIMB",
        "chineseTraditional" : "CIMB",
        "english" : "CIMB",
        "japanese" : "CIMB",
        "korean" : "CIMB"
    }
    }
}
"""
if let jsonD = json.data(using: String.Encoding.utf8) {
    do {
        if let data = try JSONSerialization.jsonObject(with: jsonD, options: JSONSerialization.ReadingOptions.mutableLeaves) as? [String: [String: [String: String]]] {
            print(data["language"]!["A00001"]!["english"]!)
        }
    } catch let error {
        print(error.localizedDescription)
    }

}

Here is another answer using Swift 4's Codable API. I thinks it's worth trying if your code needs type safety and you are sure the fields in JSON are consistent.

let json = """
{
"language": {
"A00001": {
"chineseSimplified" : "CIMB",
"chineseTraditional" : "CIMB",
"english" : "CIMB",
"japanese" : "CIMB",
"korean" : "CIMB"
}
}
}
"""
struct MyLanguages:Codable {
    struct LanguageGroup: Codable {
        struct Language:Codable {
            var chineseSimplified: String
            var chineseTraditional: String
            var english: String
            var japanese: String
            var korean: String
        }
        var A00001: Language

    }
    var language: LanguageGroup
}
if let jsonD = json.data(using: String.Encoding.utf8) {
    let jsonDecoder = JSONDecoder()
    do {
        let myLang = try jsonDecoder.decode(MyLanguages.self, from: jsonD)
        print(myLang.language.A00001.chineseSimplified)

    }
    catch let err {
        print(err.localizedDescription)
    }

}
Rikesh Subedi
  • 1,755
  • 22
  • 21
  • Thanks for answering. Anyhow, I want JSON decode in any kind of format. not fix to 1 formatting like struct or [ String: [String: String] ]. – Txuver Jhi Wei Mar 13 '18 at 04:41