8

NSData* jsonData is the http response contains JSON data.

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

I got the result:

{ "result": "\u8aaa" }

What is the proper way to encoding the data to the correct string, not unicode string like "\uxxxx"?

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365
shiami
  • 7,174
  • 16
  • 53
  • 68
  • Did u try NSString* jsonString = [[NSString alloc] initWithData:jsonData]; ? – iCoder Jun 27 '13 at 08:05
  • 1
    How exactly did you print `{ "result" = "\U8aaa"; }` ? That looks (almost) like the NSLog of a dictionary. Do you already use a JSON parser (e.g. NSJSONSerialization) ? – Martin R Jun 27 '13 at 08:26
  • This is tough because it should be \u not \U. Is there anything you can do about that? – borrrden Jun 27 '13 at 08:26
  • @MartinR: I used AFNetworking to request. And then get the responseData. Not yet use a JSON parser. I tried JSONKit to parse but it also get the same result. – shiami Jun 27 '13 at 09:19
  • @borrrden: Yes, you are right. It is lowercase 'u'. I edited my question. Thanks. – shiami Jun 27 '13 at 09:20
  • @shiami: Is that really the output of `jsonString`? I just wonder because JSON would look like `{ "result" : "\u8aaa" }`. – Martin R Jun 27 '13 at 09:24
  • @shiami: Perhaps you can show more code, starting with the AFNetworking request. – Martin R Jun 27 '13 at 09:32
  • @MartinR: Yes, I just printed with dictionary. Now I edited to the correct string. – shiami Jun 27 '13 at 09:45

2 Answers2

19

If you convert the JSON data

{ "result" : "\u8aaa" }

to a NSDictionary (e.g. using NSJSONSerialization) and print the dictionary

NSError *error;
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
NSLog(@"%@", jsonDict);

then you will get the output

{
    result = "\U8aaa";
}

The reason is that the description method of NSDictionary uses "\Unnnn" escape sequences for all non-ASCII characters. But that is only for display in the console, the dictionary is correct!

If you print the value of the key

NSLog(@"%@", [jsonDict objectForKey:@"result"]);

then you will get the expected output

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
0

I don't quite understand what the problem is. AFNetworking has given you a valid JSON packet. If you want the above code to output the character instead of the \u… escape sequence, you should coax the server feeding you the result to change its output. But this shouldn't be necessary. What you most likely want to do next is run it through a JSON deserializer…

NSDictionary * data = [NSJSONSerialization JSONObjectWithData:jsonData …];

…and you should get the following dictionary back: @{@"result":@"說"}. Note that the result key holds a string with a single character, which I'm guessing is what you want.

BTW: In future, I suggest you copy-paste output into your question rather than transcribing it by hand. It'll avoid several needless rounds of corrections and confusion.

Marcelo Cantos
  • 181,030
  • 38
  • 327
  • 365