Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I read the data from a json string? iphone

I have in a NSString "[{"van" : 1,312, "vuan":12,123}] and in order to get this values for every key, I am doing this:

    NSData *data1 = [jsonResponse1 dataUsingEncoding:NSUTF8StringEncoding];
    jsonArray = [NSJSONSerialization JSONObjectWithData:data1 options:kNilOptions error:&err];

    self.van = [NSMutableArray arrayWithCapacity:1];
    self.vuan = [NSMutableArray arrayWithCapacity:1];

    for (NSDictionary *json in jsonArray) {
        NSString * value = [json objectForKey:@"van"];
        [self.van addObject:value];
        lbl1.text = value;

        NSString * value1 = [json objectForKey:@"vuan"];
        [self.vuan addObject:value1];
        lbl4.text = value1;
    }

May be I don't have to use an array and instead to convert the NSData directly in a NSDictionary.

Anyway, I don't understand why jsonArray is nil, although jsonResponse1 contains the values I have written above.

EDIT: My boss have written the json string wrong. Thank you all for your suggestions!:)

like image 394
Maria Stoica Avatar asked Dec 08 '22 17:12

Maria Stoica


1 Answers

Your JSON is invalid. Fix it. This site is your friend.

http://jsonlint.com/

like image 137
borrrden Avatar answered Dec 11 '22 09:12

borrrden