D. Rao D. Rao - 5 months ago 45
iOS Question

iOS: SwiftyJSON parsing from Alamofire is an array. Getting null when trying to parse

the api request returns something like this:

[
{
"macAddress":"x:x:x:x",
"hatInfo":{
"hatHierarchyString":"hello",
"floorRefId":"xxx",
.....


I am using swiftyJSON to parse this, I got data from Alamofire request:

let json = JSON(data)
let mapHeir = json[0]["hatInfo"]["hatHierarchyString"]
print(mapHeir)


This returns null, I tried:

let mapHeir = json[0]["hatInfo"]["hatHierarchyString"].string
let mapHeir = json[0]["hatInfo"]["hatHierarchyString"].stringValue
let mapHeir = json["hatInfo"]["hatHierarchyString"]


and all of them return empty.
How can I get to hatHierarchyString?

Answer

Try this:

Suppose you've something like:

.responseJSON{ response in

switch response1.result{
                    case .Success:
            let json = JSON(data: response.data!)

Then do,

if let someThing = json[0]["hatInfo"]["hatHierarchyString"].string {
  //Now you got your value
}