-3

I am working on NSString where I am getting this string

 "[  {    \"36\" : 142  }",
        "  {    \"38\" : 149  }",
        "  {    \"39\" : -1  }",
        "  {    \"40\" : -1  }",
        "  {    \"41\" : 163  }",
        "  {    \"42\" : -1  }",
        "  {    \"84\" : 302  }]"

I want to remove all \ from this string.

Thanks in advance.

Kumar
  • 1,882
  • 2
  • 27
  • 44
Pradumna Patil
  • 2,180
  • 3
  • 17
  • 46
  • 1
    possible duplicate of [Objective-C - How To Remove Characters From a String?](http://stackoverflow.com/questions/2289928/objective-c-how-to-remove-characters-from-a-string) – MeanGreen Jul 07 '15 at 06:39
  • first it is not a string its a dictionary and you have " " in string, first parse your string, then remove \ from individual strings . "stringByReplacingOccurrencesOfString" that doesn't work directly for this – Code cracker Jul 07 '15 at 06:43
  • It looks like escaped JSON inside of string literals separated with commas, probably inside of array literal? Could you provide more details on the usage? – Tricertops Jul 07 '15 at 06:49
  • Yes I have searched but not found satisfied answer that is why I posted question. – Pradumna Patil Jul 07 '15 at 06:54
  • @PradumnaPatil Have you tried my answer.? – V.J. Jul 07 '15 at 06:58
  • @PradumnaPatil please update your question. – Kumar Jul 07 '15 at 07:10
  • Please write full code where you want to replace the \. Not just a string – V.J. Jul 07 '15 at 07:15

3 Answers3

2

You have to replace '\' character with NULL character.

NSString *newString = [myString stringByReplacingOccurrencesOfString:@"\\" withString:@""];
Kumar
  • 1,882
  • 2
  • 27
  • 44
1

Approach One:

string = [string stringByReplacingOccurrencesOfString:@"\\" withString:@""];

Approach Two:

NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

I'm curious if approach 2 works for parsing the string into a json?

etayluz
  • 15,920
  • 23
  • 106
  • 151
1

You need to replace \\ with BLANK SPACE using stringByReplacingOccurrencesOfString method. Below is the example...

NSString *yourString = [yourString stringByReplacingOccurrencesOfString:@"\\" withString:@""];
V.J.
  • 9,492
  • 4
  • 33
  • 49