Monday 7 April 2014

How to convert JSON to NSString and NSString to JSON Data?

How to convert NSDictionary to NSString which contains JSON of NSDictionary or NSMutableArray ?

This trick is also usefull for insert the JSON of NSMutableArray or NSMutableDictionary to Sqlite Database.

Example :

Check this : http://stackoverflow.com/questions/14864088/how-to-convert-nsdictionary-to-nsstring-which-contains-json-of-nsdictionary

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:
                                             jsonObject
                                        options:0
                                        error:nil];
NSString *node_array_val = [[NSString alloc] initWithData:jsonData
                                             encoding:NSUTF8StringEncoding];
 
Now Add this string to Sqlite Databse Table. 
And How to get this string and convert to this string to 
NSMutableArray or NSMutabledictionary : 
 
Example : 

Check this  : http://stackoverflow.com/questions/18736250/converting-nsstring-to-nsdictionary-json





NSString *jsonString = @"{\"ID\":{\"Content\":268,\"type\":\"text\"},
                          \"ContractTemplateID\":{\"Content\":65,\"type\":\"text\"}}";
 
NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
Now if you do following NSLog statement

NSLog(@"%@",[json objectForKey:@"ID"]);
Result would be another NSDictionary.

{ Content = 268; type = text; }
 
 
 
 
 
 
 
 

No comments:

Post a Comment