OGeek|极客世界-中国程序员成长平台

标题: ios - 从在线 JSON 文件更新 JSON 本地文件 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 06:31
标题: ios - 从在线 JSON 文件更新 JSON 本地文件

当有新版本的文件时,我想在应用程序中更新我的本地 JSON 文件。我有检查是否有新版本的算法。

但是现在,我需要一些代码来更改/更新在线 JSON 的本地 JSON。



Best Answer-推荐答案


我建议您使用 .plist 文件,因为它可以轻松存储 NSDictionary 或 NSArray(可以从 JSON 对象翻译)

试试下面的代码:

//store plist file in documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = paths[0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent"localJSON.plist"];
if([responseObject isKindOfClass:[NSArray class]])
{
//its an array
NSArray * dataToStore =  jsonObject;
[dataToStore writeToFile:filePath atomically:YES];
} 
else
{
//its a dictionary
NSDictionary * dataToStore =  jsonObject;
[dataToStore writeToFile:filePath atomically:YES];
}

关于ios - 从在线 JSON 文件更新 JSON 本地文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29769235/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4