Showing posts with label NSJSONSerialization. Show all posts
Showing posts with label NSJSONSerialization. Show all posts

Wednesday, January 13, 2016

Objective C parsing JSON response



We all know that JSON is the most suitable data format for to and fro of the data through mobile services as it is light weight and the format is pretty easy to read and write.

In this post let's discuss on how to parse JSON response from server. 

We got a response from the service in NSData format and we need to parse it, that means need to convert in any of the below listed readable and writable formats as we can't directly play withe NSData format of the response.

  • NSString
  • NSDictionary
  • NSArray


We can do it using the same NSJSONSerialization class which we used in my earlier post to prepare a JSON string Object.

 id *response = [NSJSONSerialization JSONObjectWithData:responseData    
                                       options:NSJSONReadingAllowFragments error:&error];


At the server end, backend developers can send the data in dictionary format or in an array format. So until unless we have a perfect handshake between backend developer, we can't assume the received data format.

As we can't assume the received data format, I have taken it into an id object. Now, it is a good practice and also often it is mandatory to check the received data format from server to play with the data.

We can check the data format using the NSObject introspection methods like shown below.


if ([response isKindOfClass:[NSDictionary class]]{

    NSDictionary *responseDict = (NSDictionary*)response;
}

if ([response isKindOfClass:[NSArray class]]{

    NSArray *responseArr = (NSArray*)response;
}

if ([response isKindOfClass:[NSString class]]{

    NSString *responseStr = (NSString*)response;
}


Last but not the least, let's see what is that option parameter here. Here are the different JSON reading options.

 NSJSONReadingMutableContainers = (1UL << 0),
 NSJSONReadingMutableLeaves = (1UL << 1),
 NSJSONReadingAllowFragments = (1UL << 2)



Hope this post is useful. Feel free to comment incase of any queries.

Objective C preparing JSON object



We all know that JSON is the most suitable format for to and fro of the data through mobile services. Because, It is light weight and the format is easy to read and write.

In this post let's discuss on how to prepare a JSON object from a list of information whether it's a key based information (NSDictionary) or a index based information (NSArray).

Let's say we have user profile information in a dictionary as shown below.

NSDictionary *userInfoDict = [NSDictionary alloc] initWithObjectsAndKeys:

@"Karunakar",@"firstname",
@"Bandikatla",@"lastname",
@"Male",@"gender",
@"India",@"country",
@"Degree",@"education",

nil];

We need to send the above information to a service. Obviously, we can't send a dictionary object to a service. We need to convert it to string format and on the server side this information should be parsable. So we need to convert it to a JSON string.

Let's see how to convert a dictionary information to a JSON string using Objective C.

Using NSJSONSerialization, we can convert this dictionary to NSData and then we can pass it to service or we can convert this to NSString from NSData and then pass it as a JSON formatted string as shown below.

NSData *jsonData = [NSJSONSerialization dataWithJSONObject: userInfoDict
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:&error];

Sometimes, we may even need to upload user information or some other information as a file to the server. In this case we can directly give NSData to file upload service. We need to check the error value to make sure things are fine before sending it to the server.

Last but not the least, let's see what is that 'options' parameter here. If we are uploading this user info file to the server, If we open that file, If we want that file to be in a JSON readable format like shown below, the option should be NSJSONWritingPrettyPrinted. If you are not bothered about the pretty writing format, you can ignore that parameter.














Hope this post is useful. Feel free to comment incase of any queries.