我有一个从JSON创建的NSDictionary:
bookDictionary = [NSJSONSerialization JSONObjectWithData: bookData
options: NSJSONReadingMutableContainers
error: &anError];
每本书都有很多资产,每个资产都有一层。最多有3层。 资产可以包含一种文本或图像。
我正在努力完成两件事。首先创建3个新词典,layer1,layer2和layer3,并使用每个图层的资源填充它们。
对于我想要下载并保存图像供以后使用的每个图像资源,这是我用来保存图像的代码:
NSString *stringURL = [NSString stringWithFormat:@"URL/%@",[assetData objectForKey:@"content" ]];
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"filename.png"];
[urlData writeToFile:filePath atomically:YES];
并且对于每个Image资产在本地保存之后我需要用filePath替换url。
建议会非常有用,如果有人对NSDictionaries有类似操作的示例代码会很棒
来自JSON请求的示例数据:
"average rating":"4",
"# of ratings":"41",
"list price":"43.99",
"your price":"43.15",
"thumburl":"/img/avatar.png",
"assets":[
{
"id":"8",
"type":"Text",
"content":"T-Bar%20Controller",
"layer":"layer-1",
"top":"190",
"left":"74",
"width":"130",
"height":"28",
"action":"",
"url":""
},
{
"id":"9",
"type":"Text",
"content":"Scroller",
"layer":"layer-1",
"top":"304",
"left":"804",
"width":"118",
"height":"27",
"action":"",
"url":""
},
"id":"63",
"type":"Image",
"content":"ugc/ronny/25/assets/pho.JPG",
"layer":"layer-2",
"top":"0",
"left":"0",
"width":"340",
"height":"191",
"action":"",
"url":""}