0

I am getting a json response from a web service and sticking it in a nsdictionary. Some characters like ™ and ® are coming across as Unicode characters. Is there an easy way to convert those characters to UTF8 so they don't display with weird characters in the UI?

Edit: I am using JSONKit to parse the JSON to dictionarys. The JSON looks like the following from the web service.

{
    "Name":"Test â¢"
}

When I log the dictionary I get Name = "Test \U00e2\U0084\U00a2";

- (void)getProductsFavorties:(NSDictionary *)params
{

    [self setNetworkQueue:[ASINetworkQueue queue]];
    [[self networkQueue] setQueueDidFinishSelector:@selector(queueFinished:)];

    NSString *getProductsFavortiesURL = [NSString stringWithFormat:@"%@%@", GET_PRODUCTS_FAVORTIES, [params objectForKey:@"loginkey"]];
    NSLog(@"getProductsFavortiesURL: %@", getProductsFavortiesURL);
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:getProductsFavortiesURL]];

    [request setDelegate:self];
    [request setDidFinishSelector:@selector(getProductsFavortiesFinished:)];
    [request setDidFailSelector:@selector(getProductsFavortiesFailed:)];

    [request addRequestHeader:@"Content-Type" value:@"application/json"];
    [request addRequestHeader:@"Accept" value:@"application/json"];
    [request setRequestMethod:@"GET"];
    [request setTimeOutSeconds:10];
    [[self networkQueue] addOperation:request];
    [[self networkQueue] go];
}

- (void)getProductsFavortiesFinished:(ASIHTTPRequest *)request
{
        if ([[self networkQueue] requestsCount] == 0) {
            [self setNetworkQueue:nil];
        }

        // Handle success
        NSString *theJSON = [request responseString];

        NSDictionary *productFavouriteResponse = [theJSON JSONValue];
        if ([productFavouriteResponse objectForKey:@"AuthResponse"] && [productFavouriteResponse objectForKey:@"data"] && [productFavouriteResponse objectForKey:@"data"] != [NSNull null]) {
            [[NSNotificationCenter defaultCenter] postNotificationName:GET_PRODUCTS_FAVORTIES_SUCCESS_NOTIFICATION object:productFavouriteResponse];
        } else {
            [[NSNotificationCenter defaultCenter] postNotificationName:GET_PRODUCTS_FAVORTIES_FAILURE_NOTIFICATION object:productFavouriteResponse];
        }

}

Now I can get the result dictionary value from [productFavouriteResponse objectForKey:@"Name"] and stick that into a uilabel and I get "Test â¢"

Erik Rodriguez
  • 145
  • 1
  • 10
  • It sounds like you're doing some work to get the json into the dictionary. Is there any reason you aren't using a preexisting library like JSONKit that would do that work for you? They generally handle encoding for free, and can have significant performance advantages. – Farski Dec 13 '13 at 05:08
  • @farski I am using JSONKit to do this. – Erik Rodriguez Dec 13 '13 at 05:12
  • It's strange that they would not be displaying correctly then. Seeing the raw response from the service may help give a clue. – Farski Dec 13 '13 at 05:14
  • Those characters match that data; U00e2 is â in UTF8. What are you expecting that sting to be? – Farski Dec 13 '13 at 05:49
  • I am expecting it to be ™ (trademark) – Erik Rodriguez Dec 13 '13 at 05:51
  • 1
    Seems like maybe the web service is incorrectly reporting its encoding. \u2122 would be a ™. \U00e2\U0084\U00a2 is the UTF8 hex value for TM (e284a2) malformed to other characters. What are you using to get the data from the service? – Farski Dec 13 '13 at 06:04
  • Oops - Reading the question again it does not seem to be a duplicate - sorry about that!!!! – Martin R Dec 13 '13 at 06:21
  • It could also be a encoding/conversion problem on the client side. How do you read the JSON response, and how do you convert it to a string? – Martin R Dec 13 '13 at 06:31
  • "The JSON **looks like** the following from the web service.". No, don't bother with the looks. Check the bytes. – R. Martinho Fernandes Dec 13 '13 at 10:55
  • @MartinR I'm using JSONKit to convert the response to a dictionary. – Erik Rodriguez Dec 13 '13 at 14:42
  • @farski We are still using ASIHTTPRequest to interact with our web services. – Erik Rodriguez Dec 13 '13 at 14:44
  • @ErikRodriguez: It would really be helpful to show some code. Then we have some point where we can start to look for your problem. – Martin R Dec 13 '13 at 18:13
  • @MartinR updated with my code. – Erik Rodriguez Dec 14 '13 at 02:32
  • @ErikRodriguez: I am not familiar with ASIFormDataRequest, but I strongly suspect that the JSON data from the server is converted with NSASCIIStringEncoding instead of NSUTF8StringEncoding (that would produce exactly your "Test â¢"). Perhaps there is an option to set the encoding to use. – Martin R Dec 14 '13 at 09:03
  • I found the solution to this on http://stackoverflow.com/questions/1775859/how-to-convert-a-unichar-value-to-an-nsstring-in-objective-c ... I created a category for this. – Erik Rodriguez Dec 19 '13 at 20:55

0 Answers0