Reputation: 4247
I'm receiving a JSON response and I need to escape quotation marks. This is The string I get:
"sign":0,"text":"Continue onto William Elton \"Brownie\" Brown Freeway, I 580"
Now, I need to get the string looking like this:
\"sign\":0,\"text\":\"Continue onto William Elton \"Brownie\" Brown Freeway, I 580\"
But, when I call this method
sectionString = [sectionString stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
What I get is:
\"sign\":0,\"text\":\"Continue onto William Elton **\\"Brownie\\"** Brown Freeway, I 580\"
So the problem is with the bold part, Brownie, that already had quotation marks, before I started replacing them. How can I solve this, and only escape " and not \" as well?
Upvotes: 0
Views: 195
Reputation: 5935
To do what you asked for, first un-escape the escaped quotes:
Use stringByReplacingOccurrencesOfString to change all the \" into "
sectionString = [sectionString stringByReplacingOccurrencesOfString:@"\\\"" withString:@"\""];
Then use stringByReplacingOccurrencesOfString to change all the " into \"
sectionString = [sectionString stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
Upvotes: 1