Has anyone been able to find a way to parse through JSON files in Swift 3? I have been able to get the data to return but I am unsuccessful when it comes to breaking the data down into specific fields. I would post sample code but I've gone through so many different methods unsuccessfully and haven't saved any. The basic format I want to parse through is something like this. Thanks in advance.
{
"Language": {
"Field":[
{
"Number":"976",
"Name":"Test"
},
{
"Number":"977",
"Name":"Test"
}
]
}
}
In Xcode 8 and Swift 3
id
now imports asAny
rather thanAnyObject
This means that
JSONSerialization.jsonObject(with: data)
returnsAny
. So you have to cast the jsondata
to a specific type like[String:Any]
. Same applies to the next fields down the json.In practice you would probably want some specific field buried in the json. Lets assume it's the
Name
field of the first element ofField
array. You can use a chain of unwraps like this to safely access the field:Also you may want to check Apple's Swift Blog Working with JSON in Swift
Use swiftjson library I think its very easy way to parse
....
like this .