-2

From webservice i get the data as json but the problem is the json comes with all " with a \ ie "all" comes as \"all\"

How to make this a valid json and then a dictionary?

{
    GetDataResult = "[{\"www\":{\"0\":{\"ID\":\"10233\",\"Queue\":\"COMPLETED\",\"EstCommName\":\"\U062e\U0631\U0645 \U0644\U0644\U0627\U0644\U0648\U0645\U0646\U064a\U0648\U0645 \U0648\U0627\U0644\U0632\U062c\U0627\U062c\",\"ReturnTime\":\"\",\"Latitude\":\"\",\"Longitude\":\"\"},\"1\":{\"ID\":\"10304\",\"Queue\":\"COMPLETED\",\"EstCommName\":\"\U0627\U062d\U0645\U062f \U0627\U0644\U0643\U0646\U062f\U064a \U0644\U0644\U0627\U0644\U0645\U0648\U0646\U064a\U0648\U0645 \U0648\U0627\U0644\U0632\U062c\U0627\U062c\",\"ReturnTime\":\"\",\"Latitude\":\"\",\"Longitude\":\"\"},\"2\":{\"ID\":\"10667\",\"Queue\":\"FRESH\",\"EstCommName\":\"\U0645\U0646\U062c\U0631\U0629 \U0627\U0644\U062e\U0632\U0646\U0629\",\"ReturnTime\":\"\",\"Latitude\":\"\",\"Longitude\":\"\"},\"3\":{\"ID\":\"10777\",\"Queue\":\"FRESH\",\"EstCommName\":\"\U0645\U0624\U0633\U0633\U0647 \U062c\U0647\U0627\U0645 \U0644\U0627\U0639\U0645\U0627\U0644 \U0627\U0644\U0633\U064a\U0631\U0627\U0645\U064a\U0643\",\"ReturnTime\":\"\",\"Latitude\":\"\",\"Longitude\":\"\"}}},{\"asd\":{}},{\"ssd\":{}}]";

In other words

TLDR

how to remove \ from a word \"hello\".? ie output needed is "hello".

What i tried

     NSLog(@"%@",[[op objectForKey:@"GetSampleDataResult"] stringByReplacingOccurrencesOfString:@"\"" withString:@""]);
Lithu T.V
  • 19,955
  • 12
  • 56
  • 101

4 Answers4

1

I have not tried this but something like this can work for you.

Sample Code :

NSString *yourString = [yourJSON objectForKey:@"GetDataResult"];
NSData *data = [yourString dataUsingEncoding:NSUTF8StringEncoding];
NSError *error = nil;
NSArray *www = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
NSLog(@"www :: %@",www);
Bhavin
  • 27,155
  • 11
  • 55
  • 94
  • thats not the way to convert a string – Lithu T.V Oct 17 '13 at 09:39
  • 1
    So the problem was not with `\"`. The problem was, that when you printed out the JSON string the _output_ contained those escapes, not the _actual_ string. – Tricertops Oct 17 '13 at 09:42
  • 2
    Now it looks correct, and using NSJSONSerialization is better than any string manipulation, because the strings inside the nested JSON could have escaped quotation marks or colons ... – Martin R Oct 17 '13 at 09:42
  • 1
    By the way, that JSON string contains Array as the root type. You used `NSDictionary`. – Tricertops Oct 17 '13 at 09:52
  • @iMartin: Yeah. Thanks for correcting. I updated it to `NSArray`. – Bhavin Oct 17 '13 at 10:01
0

from the top of my head, to replace \" you need to specify it as \\\"

reason being that \ itself is used as an escape character and thus you need to escape:

  • \ with \\
  • " with \"

but the quotes are needed so you need to rid yourself of the \
so this:

NSLog(@"%@",[[op objectForKey:@"GetSampleDataResult"]
             stringByReplacingOccurrencesOfString:@"\\"
             withString:@""]);

but as mentioned, NSJSONSerialization is the best way to go about this

staticVoidMan
  • 19,275
  • 6
  • 69
  • 98
0
[string stringByReplacingOccurrencesOfString:@"\\\"" withString:@"\""];

Be careful with quotation marks and backslashes.

  • Result of \\\" will be \", exactly what you need to find.
  • Result of \" will be ", exactly what you need to replace.

Edit: This is answer to “How to remove \ from a word \"hello\"?”, not a solution to the problem.

Tricertops
  • 8,492
  • 1
  • 39
  • 41
0

I tried like this below:--

NSString *word= @"'\'hello'\'";
    NSArray *arr=[word componentsSeparatedByString:@"'\'"];
    for (NSString *str in arr)
    {
        if ([str length] >0)
        {
        NSLog(@"%@",str);
        }
    }
Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56