1

I am accessing a CMS with a pre-baked JSON output and it uses the following data structure. I am struggling to get down into the nested collections to get the version or loop through the categories.

{
    results: [
        {
            pageData: {
                info: {
                    version: "1"
                },
                categories: [
                    {
                        name: "Cat 1"
                    },
                    {
                        name: "Cat 2"
                    }
                ]
            }
        }
    ]
}

Here is the code I was attempting to use. Any help is appreciated!

guard let json = json, let results = json["results"], let pageData = results["pageData"], let info = pageData["info"] as? [String:Int], let version = info["version"], 
    let categories = Category.getCategories(json: json) else {
    self.completionParse(RequestResult.errorParsing, self.categoriesResult)
    return
}
5
  • Your code indicates you understand how to dig into a dictionary to get the desired data. What issue are you having trying to obtain the slightly different data? Commented Feb 23, 2017 at 4:50
  • @rmaddy If I remove results and pageData collections and bring the info and categories collection to the top level the code above works. If I continue the let sequencing, and try and start at results and walk down it errors out and doesn't seem to work. Trying to determine if there is a better way to do this honestly. Commented Feb 23, 2017 at 4:53
  • You're ignoring the "results" and "pageData" fields, which contain the actual data. Commented Feb 23, 2017 at 4:54
  • 1
    Update your question with your attempt to access the desired data below results and pageData. Commented Feb 23, 2017 at 4:56
  • @rmaddy updated with the additional I was trying Commented Feb 23, 2017 at 4:59

2 Answers 2

4

To access the info and categories dictionaries you need to first access results Array and pageData that is inside the first object of results array.

guard let json = json, let results = json["results"] as? [[String:Any]], 
    let firstDic = results.first, let pageData = firstDic["pageData"] as? [String:Any],
    let info = pageData["info"] as? [String:Int], let version = info["version"], 
    let categories = Category.getCategories(json: pageData) else {

    self.completionParse(RequestResult.errorParsing, self.categoriesResult)
    return
}
Sign up to request clarification or add additional context in comments.

3 Comments

This took me down the right path, thanks very much for the help.
@jeremykrall Don't get you aren't you able to get version and categories with my solution or Am i missing something?
Ended up with ` guard let json = json, let results = json["results"] as? [[String:Any]], let result = results.first, let pageData = result["pageData"] as? [String:Any], let info = pageData["info"] as? [String:Int], let version = info["version"], let categories = Category.getCategories(json: pageData) else { self.completionParse(RequestResult.errorParsing, self.categoriesResult) return } `
2

Tested below code on playground. This code is in latest swift 3.

if let dictResponse = json as? [String:Any] {
    // This will get entire dictionary from your JSON.
    if let results = dictResponse["results"] as? [[String:Any]]{
        if let pageData = results.first? ["pageData"] as? [String:Any]{
            if let info = pageData["info"] as? [String:Any]{
                if let version = info["version"] as? String{
                    print(version)
                    // This will print 1
                }
            }

            if let categories = pageData["categories"] as? [[String:Any]]{
                // This will give you a category array. Later on you can iterate and get the dictionary’s value of each element.
                for categoriesObj in categories.enumerated(){
                    if let name = categoriesObj.element["name"]{
                        print(name)
                    }
                }
            }           
        }   
    }
}

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.