Search code examples
jsonswiftalamofireswifty-json

Parsing JSON value with SwiftyJSON (and Alamofire)


I am trying to parse a single value from a REST web service that I am testing. I understand how to make the call and I see the JSON response in the Output window.

let request = Alamofire.request(.GET, "http://IP:PORT/jsonTest", parameters: ["s": "Ping?"])
        .responseJSON{(_,_,data,_) in             
            var json = JSON(data!)                
            println(json)

The Console Output shows me:

{"NewDataSet":[
    {"Table1":[
        {"Column-A":"FirstA",
         "Column-B":"FirstB"
        },
        {"Column-A":"SecondA",
         "Column-B":"SecondB"
        },
        {"Column-A":"ThirdA",
        "Column-B":"ThirdB"
        }
    ]}
]}

What I would like to do now, is to display only the first value from Column-A - which in this example would be "FirstA".

I've been trying to use a code like this, but so far I am not getting anywhere...

println(json[0][0]["Column-A"].stringValue)

Any pointers much appreciated!


Solution

  • json["NewDataSet"][0]["Table1"][0]["Column-A"].stringValue
    

    This is what you want. This is because your json starts with a dictionary and is formatted as dictionary>array>dictionary>array>dictionary. Note that json dictionaries are noted by { : , : } while arrays are noted as [ , ].