8

NSData* jsonData是包含 JSON 数据的 http 响应。

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

我得到了结果:

{ "result": "\u8aaa" }

将数据编码为正确字符串的正确方法是什么,而不是像“\uxxxx”这样的unicode字符串?

4

2 回答 2

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

于 2013-06-27T09:55:33.813 回答
0
于 2013-06-27T09:39:51.893 回答