How can I parse a JSON array into objective-c

I made it easy in Android, but Im a bit lost on how to do it in iOS. My json is like:

[{"name":"qwe","whatever1":"asd","whatever2":"zxc"}, 
{"name":"fgh","whatever1":"asd","whatever2":"zxc"}]

      

If I do this:

NSData *jsonData = [data dataUsingEncoding:NSUTF32BigEndianStringEncoding];

rows  = [NSJSONSerialization JSONObjectWithData: jsonData options: NSJSONReadingMutableContainers error: &error];

      

in the lines I can access with?

NSString *name = [[rows objectAtIndex:0] [objectForKey:@s"name"]];

      

Or how do I do it? thank.

FINALLY NSString *name = [[rows objectAtIndex:0] objectForKey:@"name"]];

WORK !: D

+3


source to share


3 answers


I think this will work, but you want:

NSString *name = [[rows objectAtIndex:0] objectForKey:@"name"];

      



(stripped away extraneous square brackets and use @"name"

as string literal).

However, is the JSON input really UTF-32?

+3


source


NSMutableArray *row= [NSJSONSerialization JSONObjectWithData: jsonData options:        NSJSONReadingMutableContainers error: &error];
        for (int i=0; i<[row count]; i++) {
            NSMutableDictionary *dict=[row objectAtIndex:i];;
            NSString * name=[dict valueForKey:@"name"];
            NSLog(@"%@",name);
        }
    }

      



+2


source


Assuming the operation NSJSONSerialization

was successful, simply:

NSString *name = rows[0][@"name"];

      

NSUTF32BigEndianStringEncoding

is suspicious, json data most often NSUTF8StringEncoding

.

+2


source







All Articles