JSON serialization double quote (") and single (') quote issue in iOS

1.5k Views Asked by At

I'm facing JSON encoding issue.

I have submitted my data to server on below formate to save remarks.

{
Remarks = "test apple """ ";
}

Then while fetching the data from the server, I'm receiving different formate like output:

{
Remarks = "test apple \U00e2\U0080\U009c\U00e2\U0080\U009d\U00e2\U0080\U009d\U00e2\U0080\U009d\\n";
}

While submitting data I'm using JSON serialisation.

  NSData *jsonData
    = [NSJSONSerialization dataWithJSONObject: dict
                                      options: NSJSONWritingPrettyPrinted
                                        error: nil];

  if (jsonData)
  {
    NSString* jsonString
      = [[NSString alloc]
          initWithData: jsonData
              encoding: NSUTF8StringEncoding];
    NSLog(@"posting params: %@", jsonString);
  }

My question is why I'm not getting I have submitted. I'm facing only double quote (") and single (') symbols.

Any one have idea?

2

There are 2 best solutions below

2
On BEST ANSWER

iOS 11 has added "Smart Punctuation" to the keyboard settings.

This means when typing "Singapore" it will convert it to “Singapore”. The '"' are replaced by a '“' and '”' string. Those are different quotes.

So one option might be to also replace '“' and '”' by '\"'.

Smart Punctuation = No
1
On

You probably should escape your non-delimitating quotes : { Remarks = "test apple \"\"\" "; }