Vigrant
Vigrant

Reputation: 1008

Storing JSON in an objective C NSString

NSString removes the backslashes from the JSON. I'm eventually passing the variable into another javascript function that JSON.parses it. Because the backslashes are removed, the JSON.parse is failing.

JSON = "[\"test\"]"
NSString = "[test]" (Stored as)
JSON.parse(NSString) -> fails

Upvotes: 1

Views: 116

Answers (2)

Vikas Rajput
Vikas Rajput

Reputation: 1874

use it

 NSString*  str=[[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:@[JSON] options:0 error:nil] encoding:NSUTF8StringEncoding];

Upvotes: 1

Vigrant
Vigrant

Reputation: 1008

NSData * jsonData = [NSJSONSerialization dataWithJSONObject:@[JSON] options:0 error:nil] encoding:NSUTF8StringEncoding];
NSString * jsonString = [[NSString alloc] initWithData: jsonData];

This worked for me.

Upvotes: 1

Related Questions