如何在目标c中访问值json对象

时间:2013-04-20 08:21:29

标签: ios objective-c json

我有一个关闭代码的json文件:

{"fileContent":[{"name":"directorffdsgfg","type":"file","ext":"sql","modified":"2013\/04\/11 - 10:00","created":"2013\/04\/11 - 10:00","size":"1577"},{"name":"directory02","type":"file","ext":"sql","modified":"2013\/04\/11 - 12:10","created":"2013\/04\/11 - 12:10","size":"1577"},{"name":"jquery-mousewheel-master","type":"file","ext":"zip","modified":"2013\/04\/11 - 12:10","created":"2013\/04\/11 - 12:10","size":"5213"}],"folderContent":[{"name":"Folder 2","type":"folder","ext":"sql","modified":"2013\/04\/11 - 05:04","created":"2013\/04\/11 - 05:04","size":"1577"},{"name":"Folder 1","type":"folder","ext":"zip","modified":"2013\/04\/15 - 09:08","created":"2013\/04\/15 - 09:08","size":"11867"}],"files":9,"folders":2}

我想知道对象json中的访问值是什么。 (例如,我想访问文件,文件夹,fileContent,folderContent的值)

我不知道。

1 个答案:

答案 0 :(得分:2)

JSON是一个字典,您可以使用NSJSONSerialization将此字符串转换为字典:

NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];

然后您可以按键访问属性,以获取文件夹内容数组:

NSArray *folderContent = [json objectForKey:@"folderContent"];