- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
if([(MultipleURLConnection *)connection tag]==2)
{
if([fn writeToFile:responseData fileName:@"productDetail.json"])
[self performSelector:@selector(loadingJSON) withObject:nil afterDelay:0.1];
}
}
- (BOOL)writeToFile:(NSData *)data fileName:(NSString *)fileName
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *myFile = [documentsDirectory stringByAppendingPathComponent:fileName];
////NSLog(@"writeToFile=%@",myFile);
NSFileManager *fm = [NSFileManager defaultManager];
if (![fm fileExistsAtPath:myFile]) // if file doesn't exist, create it
return ([fm createFileAtPath:myFile contents:data attributes:nil]);
else
return ([data writeToFile:myFile atomically:YES]);
}
}
当我在我的应用程序中有json文件时,上面的鳕鱼工作正常,并在应用程序中显示文件的所有数据。现在我决定将此json文件保留在服务器上然后使用该URL我想从json文件中获取数据。从服务器获取数据我尝试了一些方法,如
NSURL *url = [NSURL URLWithString:@"http://mywebsite.com/productDetail.json"];
NSData *jsonData = [NSData dataWithContentsOfURL:url];
if(jsonData != nil)
{
NSError *error = nil;
id result = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
if (error == nil)
NSLog(@"%@", result);
}
当我检查nslog结果时,它会显示我在productDetail.json中的所有数据但是 现在这里有问题,我需要在文件名中填充数据,或者从json文件获取数据后如何修改下面的行
if([fn writeToFile:responseData fileName:@""])
任何建议都会被推荐。谢谢