Nan
Nan

Reputation: 524

JSON parse content, result is unreadable, such as ""\U5b9d\U4e30""

- (IBAction)updating:(id)sender {


    NSString *string = [NSString stringWithFormat:@"https://api.heweather.com/x3/weather?cityid=CN101180503&key=MY_API_ID"];
    NSURL *url = [NSURL URLWithString:string];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    operation.responseSerializer = [AFJSONResponseSerializer serializer];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

        //ResponseObject is dictionary
        NSMutableDictionary *dictionary = responseObject;


        NSArray *array = dictionary[@"HeWeather data service 3.0"];


        NSDictionary *dicBasic = [array valueForKey:@"basic"];

        NSData *city = [dicBasic valueForKey:@"city"];
        NSLog(@"%@", city);


        NSString *latitude = [dicBasic valueForKey:@"lat"];
        NSLog(@"%@", latitude);




    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
        NSLog(@"Nothing ");
    }];

When i RUN the piece code, every this is normal, apart from city's name,

I think i need to decode the city's name from the JSON file, as the city's name is in Chinese.

Here's an example:

2016-07-23 15:36:55.825 weatherDemo[2865:1189546] ( "\U5b9d\U4e30" ) 2016-07-23 15:36:55.825 weatherDemo[2865:1189546] ( "33.908000"

How can i deal with it?

Upvotes: 1

Views: 150

Answers (2)

gnasher729
gnasher729

Reputation: 52538

First, I'd bet quite a bit of money that

dictionary[@"HeWeather data service 3.0"]

is not an array. You are then using valueForKey. Any use of valueForKey is an indication that you don't know what you are doing. valueForKey doesn't care what type of object it is. I assume that the compiler told you that you couldn't use

array [@"basic"]

and instead of fixing your blatant bug, you found the worst possible workaround.

Your actual problem is not a problem at all. JSON handles chinese characters just fine. But NSLog outputs them as you saw. That's a problem of NSLog (which is a debugging tool). The data in your string are absolutely fine. Put them into a label or a text field and they will be drawn just fine.

PS. Any time you get a response that is not what you expect your app will crash. It's absolutely irresponsible to parse JSON without checking that the items you get are actually of the type that you expect.

Upvotes: 0

will wang
will wang

Reputation: 199

After looking into the problem. I figured it out,it`s all about the data structure of response object.

Try the following code to get what you want:

    NSDictionary *responseDic = [[(NSDictionary *)responseObject objectForKey:@"HeWeather data service 3.0"] firstObject];

    NSDictionary *basicDic = [responseDic objectForKey:@"basic"];
    NSString *cityStr = [basicDic objectForKey:@"city"];
    NSLog(@"%@", cityStr);

Upvotes: 1

Related Questions