stanley
stanley

Reputation: 529

Parsing JSON data containing new line chars in a iOS application

I have a problem parsing JSON data containing new line chars (\n etc) in iOS application. I use XCode 4.3. I have added the code I used to parse JSON; the application crashes when it finds a new line char. How to replace or handle the newline char in JSON data parser or replace it in NSString?

NSString *jsonData = @"[{ "department": "","email_address1":"[email protected]"}]";    
jsonData = [jsonData stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
NSError *e = nil;
NSData *projNSdata = [jsonData dataUsingEncoding:NSUTF8StringEncoding];
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: projNSdata options: NSJSONReadingMutableContainers error: &e];

Upvotes: 2

Views: 1048

Answers (1)

Paresh Navadiya
Paresh Navadiya

Reputation: 38259

Check this modified jsonData string it working perfectly. As problem is with json string :

NSString *jsonData = @"[{\n \"department\": \"\",\"email_address1\":\"[email protected]\"}]";

Replace mine with yours.

Error is at double quote with no value :

NSString *jsonData = @"[{ "department": "","email_address1":"[email protected]"}]";  
                                       here  

Also add below line at the end:

NSLog(@"Json : %@  \n Error :%@",jsonArray,[e description]);

Upvotes: 1

Related Questions