1

I have a iPhone application which return me a string from webservice with escape characters like "\n" and "\". Now I want to add this string in nsdictionary. for that I do below

    NSMutableArray *keyArray = [[NSMutableArray alloc] initWithCapacity:1];
NSMutableArray *valueArray = [[NSMutableArray alloc] initWithCapacity:1];

[valueArray addObject:strVerifiedReceipt];
[keyArray addObject:@"PAYMENT_RECEIPT"];

NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjects:valueArray forKeys:keyArray];
NSString* jsonString = [jsonDictionary JSONRepresentation];

here jsonString return me strVerifiedReceipt with escape characters come from webservice like below

"PAYMENT_RECEIPT": "{\n\"receipt\":{\"original_purchase_date_pst\":\"2012-10-10 03:29:12 America/Los_Angeles\", \"unique_identifier\":\"977ce60f38d875d12d0f1d7fe583d1d5e61f99e8\", \"original_transaction_id\":\"1000000056917869\", \"bvrs\":\"2.0\", \"transaction_id\":\"1000000056917869\", \"quantity\":\"1\", \"product_id\":\"com.cornerstonehealthtechnologies.meanexus.Nexus010\", \"item_id\":\"544678366\", \"purchase_date_ms\":\"1349864952265\", \"purchase_date\":\"2012-10-10 10:29:12 Etc/GMT\", \"original_purchase_date\":\"2012-10-10 10:29:12 Etc/GMT\", \"purchase_date_pst\":\"2012-10-10 03:29:12 America/Los_Angeles\", \"bid\":\"com.cornerstonehealthtechnologies.meanexus\", \"original_purchase_date_ms\":\"1349864952265\"}, \"status\":0}",
Manish Jain
  • 842
  • 1
  • 11
  • 29
  • [Replace multiple characters in a string in Objective-C?](http://stackoverflow.com/a/714009/1059705) , [Replacing one character in a string - iPhone/iPad](http://stackoverflow.com/a/5223737/1059705) – Bala Oct 10 '12 at 13:13
  • have you find an option for this? I have same problem. I need to remove just "\" – Max Apr 07 '15 at 12:33

4 Answers4

1

Use stringByReplacingOccurrencesOfString:withString:

jsonString = [[[jsonString stringByReplacingOccurrencesOfString:@"\n"
                                                        withString:@""] stringByReplacingOccurrencesOfString:@"\" withString:@""]
Paresh Navadiya
  • 38,095
  • 11
  • 81
  • 132
1

stringByReplacingOccurrencesOfString:withString: will definitely work, but to remove the backslashes, make sure you put 2, because putting just one will just escape the quotation mark.

jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\\" withString:@""];
Riiibu
  • 85
  • 5
0

I have an idea to remove backslash \ from your jsonString

jsonString = [NSString stringWithFormat:@"%s",[jsonString UTF8String]]

And it is working for me and it gives me a valid JSON string.

Max
  • 2,269
  • 4
  • 24
  • 49
-1
jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\n" withString:@""];

jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\" withString:@""];
DD_
  • 7,230
  • 11
  • 38
  • 59
james lobo
  • 463
  • 4
  • 10
  • while this code block might answer the question, you should try to improve your answer by adding relevant explanation with the code see http://stackoverflow.com/help/how-to-answer – SMR Feb 27 '15 at 10:45