我有这样的json文件:
{"fileContent":[{"name":"aaaa","type":"file","ext":"sql","modified":"2013\/04\/11 - 04:29","created":"2013\/04\/11 - 04:29","size":"1460","dirid":"5"}, {"name":"directory02","type":"file","ext":"sql","modified":"2013\/04\/11 - 04:29","created":"2013\/04\/11 - 04:29","size":"1577","dirid":"6"}, {"name":"index2","type":"file","ext":"php","modified":"2013\/04\/11 - 05:06","created":"2013\/04\/11 - 05:06","size":"9932","dirid":"8"}, {"name":"directorffdsgfg","type":"file","ext":"sql","modified":"2013\/04\/11 - 10:00","created":"2013\/04\/11 - 10:00","size":"1577","dirid":"10"}, {"name":"directory02","type":"file","ext":"sql","modified":"2013\/04\/11 - 12:10","created":"2013\/04\/11 - 12:10","size":"1577","dirid":"11"},{"name":"jquery- mousewheel-master","type":"file","ext":"zip","modified":"2013\/04\/11 - 12:10","created":"2013\/04\/11 - 12:10","size":"5213","dirid":"12"},{"name":"perfect-scrollbar-master","type":"file","ext":"zip","modified":"2013\/04\/11 - 12:10","created":"2013\/04\/11 - 12:10","size":"11867","dirid":"13"}, {"name":"E_PDF","type":"file","ext":"png","modified":"2013\/04\/17 - 09:15","created":"2013\/04\/17 - 09:15","size":"7467","dirid":"24"}, {"name":"filefolder_H.PNG","type":"file","ext":"png","modified":"2013\/04\/17 - 09:15","created":"2013\/04\/17 - 09:15","size":"10575","dirid":"25"}],"folderContent": [{"name":"Folder 2","type":"folder","ext":"sql","modified":"2013\/04\/11 - 05:04","created":"2013\/04\/11 - 05:04","size":"1577","dirid":"7"},{"name":"Folder 1","type":"folder","ext":"zip","modified":"2013\/04\/15 - 09:08","created":"2013\/04\/15 - 09:08","size":"11867","dirid":"14"}],"files":9,"folders":2}
并将其存储在NSDictionary
中,并带有名称标题。 ( down代码显示:)
NSDictionary *titles = [NSJSONSerialization JSONObjectWithData:Data options:kNilOptions error:nil];
我从titles中读取了fileContent值,并将它们存储在名为“all”的NSMutableArray中,如下所示:
NSArray *fileContent = [titles objectForKey:@"fileContent"];
NSMutableArray *all = [[NSMutableArray alloc]init];
[all addObjectsFromArray:fileContent];
我希望.plist
文件中的“all”存储2值(名称和类型),但我不知道它是怎么做的?
答案 0 :(得分:1)
尝试
NSDictionary *titles = [NSJSONSerialization JSONObjectWithData:Data options:kNilOptions error:nil];
NSArray *fileContent = [titles objectForKey:@"fileContent"];
NSMutableArray *all = [fileContent mutableCopy];
NSMutableArray *folders = [NSMutableArray array];
[all enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSDictionary *folder = (NSDictionary *)obj;
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:folder[@"name"] forKey:@"name"];
[dict setObject:folder[@"type"] forKey:@"type"];
[folders addObject:dict];
}];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = paths[0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Folders.plist"];
[folders writeToFile:filePath atomically:YES];