简体   繁体   English

如何在objective-c中解析数组JSON

[英]How can I parse array JSON in objective-c

I did it easily in Android but Im little lost how to do it in iOs. 我在Android中很容易做到,但我很少失去如何在iOs中做到这一点。 My json is like: 我的json就像:

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

If I do: 如果我做:

NSData *jsonData = [data dataUsingEncoding:NSUTF32BigEndianStringEncoding];

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

in rows can I access with? 在行中可以访问吗?

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

Or how I do that? 或者我是怎么做到的? thanks. 谢谢。

FINALLY NSString *name = [[rows objectAtIndex:0] objectForKey:@"name"]]; FINALLY NSString *name = [[rows objectAtIndex:0] objectForKey:@"name"]]; WORKS! 作品! :D :d

I think that will work, however you want: 我认为这会有用,但是你想要:

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

(dropped extraneous square brackets and use @"name" as string literal). (删除无关的方括号并使用@"name"作为字符串文字)。

However, is the input JSON really in UTF-32 format? 但是,输入JSON真的是UTF-32格式吗?

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);
        }
    }

Assuming the NSJSONSerialization operation was successful, simply: 假设NSJSONSerialization操作成功,只需:

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

NSUTF32BigEndianStringEncoding is suspicious, json data is more usually NSUTF8StringEncoding . NSUTF32BigEndianStringEncoding是可疑的,json数据通常是NSUTF8StringEncoding

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM