2013-04-22 49 views
1

我JSON文件是这样的:如何存储.plist文件中解析的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与名称的标题存储此。 (下代码显示它:

NSDictionary *titles = [NSJSONSerialization JSONObjectWithData:Data options:kNilOptions error:nil]; 

,我读标题fileContent价值和他们的NSMutableArray存储与名为“所有”这样的代码:

NSArray *fileContent = [titles objectForKey:@"fileContent"]; 
NSMutableArray *all = [[NSMutableArray alloc]init]; 
[all addObjectsFromArray:fileContent]; 

我想店2值(名称&类型)从“所有”.plist文件,但我不知道它是如何做到的?

+0

的可能重复[如何从URL的JSON数据存储在iOS设备,并使用本地它(http://stackoverflow.com/questions/16141308/how-to-store- json-data-from-url-in-ios-device-and-to-use-local-it) – rckoenes 2013-04-22 08:19:46

+0

我在另一个帖子中询问不同的问题 – janatan 2013-04-22 08:33:08

回答

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]; 
+0

我编写了这个代码,但没有在plist中添加名称和类型:( – janatan 2013-04-22 09:33:08