In iOS applications, We use JSON format for web services as it is fast and easy to parse. JSON could be either in array or in dictionary format. It is a good practice to JSON format before parsing the response from a web service though we have perfect handshake with web service developers.
Let's take an example of fetching all the employees using a web service and there is a chance of JSON coming in any format as shown below.
JSON Dictionary:
{"employees":
[
{"firstName":"Tim", "lastName":"Cook"},
{"firstName":"Will", "lastName":"Smith"},
{"firstName":"Leonardo", "lastName":"Dicaprio"}
]
}
JSON Array:
[
{"firstName":"Tim", "lastName":"Cook"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]
Using introspection methods in Objective C, We can check the type of JSON.
//JSON is in Array Format
}
if([response isKindOfClass:[NSDictionary class]]){
//JSON is in Dictionary Format
}
Hope this post is useful. Feel free to comment in case of any queries.
No comments:
Post a Comment