如何将JSON文件从Web服务保存到本地JSON文件?

编程入门 行业动态 更新时间:2024-10-23 12:28:24
本文介绍了如何将JSON文件从Web服务保存到本地JSON文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我目前正在使用来自远程Web服务的JSON数据(NSArray格式)填充UITableView.我想通过调用Web服务并将JSON数据存储到本地文件来加速应用程序.

I'm currently populating a UITableView with JSON data (NSArray format) from a remote webservice. I would like to speed up the application by making a call to the webservice and storing the JSON data to a local file.

还有,这是一个好方法吗,这样用户不必一直保持下载数据的状态?

also, is this a good way to do this so the user doesn't have to keep downloading data all the time?

我遇到的问题是如何将远程JSON文件保存到本地文件.在我的-(void)saveJsonWithData:(NSData *)data 方法中,如何保存远程数据.

What I'm stuck on is how to save the remote JSON file to a local file. In my -(void)saveJsonWithData:(NSData *)data method how do I save the remote data.

这是到目前为止我正在使用的代码(来自某些Stackoverflow搜索)

Here's the code I'm using so far (from some Stackoverflow searching)

-(void)saveJsonWithData:(NSData *)data{ NSString *jsonPath=[[NSSearchPathForDirectoriesInDomains(NSUserDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingFormat:@"/data.json"]; [data writeToFile:jsonPath atomically:YES];

}

-(NSData *)getSavedJsonData{ NSString *jsonPath=[[NSSearchPathForDirectoriesInDomains(NSUserDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingFormat:@"/data.json"]; return [NSData dataWithContentsOfFile:jsonPath] }

然后将函数调用为

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { [self saveJsonWithData:data]; }

感谢帮助

推荐答案

在iOS上,您应该使用 NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)来获取文档目录.没有用户目录.

On iOS you should be using NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) to get the document directory. There is no user directory.

更多推荐

如何将JSON文件从Web服务保存到本地JSON文件?

本文发布于:2023-11-24 09:09:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1624600.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   如何将   JSON   Web

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!