Use JSON with Swifty JSON library in Swift
When we get from the API in the JSON form it is difficult to to traverse the data in swift in the form of string ,integer or in array format. To traverse the data in JSON use the SwiftyJSON library (https://github.com/SwiftyJSON/SwiftyJSON). If we don’t use the library we have to check every string in if – else statement to check that the value exists or not , And to traverse Array we use AnyObject but in the library no need of that. You have to just put SwiftyJson class and you can use the library by passing the Data type data to get the data in JSON format. Examples :-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
// get the data in json form let json = JSON(data: dataFromNetworking) // get the string value let userName = json[0]["user"]["name"].string! //[0] index of array //get value from subArray let userName = json[0]["user"]["type"][1]["gender"].string! // for double value let name = json[0].double // you can also create the json type array var array = [JSON]() |