-1

Problem: I can't parse data from a JSON file to a NSArray appropriately. UTF encoding is not working as expected.

My JSON looks something like:

[
{"Name":"Marcos","Address":"1234 Brasil Av. São Paulo - SP","Latitude":"-23.000","Longitude":"-46.70"},{"Name":"Mario","Address":"1000 Washignton Luiz Av. Itú SP","Latitude":"-20.0000","Longitude":"-46.000"}
]

My Objective-C code is:

NSError *error = nil;

NSURL *jsonUrl = [[NSURL alloc]initWithString:
    @"http://marcosdegni.com.br/teste/webservice_teste.php"];

NSString *jsonString = [NSString stringWithContentsOfURL:jsonUrl
    encoding:NSUTF8StringEncoding error:&error];

NSLog(@"jsonString: %@ , Error:%@:" ,jsonString, error); //(1)

if (!error) {
    NSError *error2 = nil;
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];

    NSArray * jsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error2];
    NSLog(@"\n\nArray: %@" \nError:$@, jsonArray, error2); //(2)

//(*1*) This log show the content's as they are expected: note the characters ã and ú on the address fields.

//(*2*) The logs from the array and the dictionary show this charters as it's UNIX codes:\U00e and \U00fa respectively.
esqew
  • 42,425
  • 27
  • 92
  • 132
  • Why are you decoding the same data twice, once as array and once as dict? And what is not working? What does `error` (which you erroneously feed `nil`) report (after you correct that bug)? – Hot Licks Nov 06 '14 at 01:57
  • This were my two attempts to get my data. Neither one worked. I've excluded the dic one for clarity. nil --> error replaced. Tks – Marcos Degni Nov 06 '14 at 02:04
  • So, in other words there's nothing wrong. The extended UNICODE data is being displayed as expected in an NSLog. – Hot Licks Nov 06 '14 at 02:05
  • No, it's still showing \U00e3 instead of ã and \U00fa instead of ú when I log the jsonArray... – Marcos Degni Nov 06 '14 at 02:14
  • Yes!! So it's correct. (Display the data in a label if you don't believe me.) – Hot Licks Nov 06 '14 at 02:35
  • Hot Licks, it worked! I've used the "self.label1.text = [[jsonArray objectAtIndex:0] objectForKey:@"Address"];" an got what I needed! Thanks for your help! – Marcos Degni Nov 06 '14 at 03:07

1 Answers1

-1

You can give this a try. The id json you get will be a NSArray, you can use it from there.

NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSArray * array = json;
for (NSDictionary *dict in array) {
    NSString *string = [dict objectForKey:@"Address"];
    NSLog(@"%@",string);
}

From here, and I get the right result if I obtain the value of the key and log it, instead of logging the NSArray directly.

Community
  • 1
  • 1
gabbler
  • 13,626
  • 4
  • 32
  • 44
  • 1
    Won't that just produce an undefined symbol message on `receivedString`? – Hot Licks Nov 06 '14 at 02:03
  • 1
    Now you're just doing the same thing as the OP. – Hot Licks Nov 06 '14 at 02:07
  • Yes, it will, It is a string I am tested for JSON library. I have removed it, thanks for pointing it out. – gabbler Nov 06 '14 at 02:08
  • 1
    How is this answer any different from the OP's code? All you have done is change the variable type from `NSArray` to `id`. – rmaddy Nov 06 '14 at 02:21
  • @rmaddy, Yes, it is the same, there are unicode characters in the array, which shows as \U format when logged out. – gabbler Nov 06 '14 at 02:35
  • 1
    Your approach also works. Somehow if I Log all the Dic it doesn't work but by logging the object for key in a dic it does. Tks – Marcos Degni Nov 06 '14 at 03:18
  • 1) So it seems the OP's code always worked. It was simply confusion caused by the logging of the array. 2) This answer states that the `id json` will be an `NSDictionary`. That is incorrect. It's an `NSArray`. – rmaddy Nov 06 '14 at 03:51