NSData to NSString with JSON response

前端 未结 2 1387
清歌不尽
清歌不尽 2020-12-06 03:09

NSData* jsonData is the http response contains JSON data.

NSString* jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8Stri         


        
2条回答
  •  死守一世寂寞
    2020-12-06 03:49

    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

提交回复
热议问题