ios-本地项目下 json文件的读取


    NSError *error;
   
// 获取文件路径
   
NSString *filePath = [[ NSBundle mainBundle ] pathForResource : @"AppConfig" ofType : @"json" ];
  
   
// 根据文件路径读取数据
   
NSData *jdata = [[ NSData alloc ] initWithContentsOfFile :filePath];
   
   
// 格式化成 json 数据
    id jsonObject = [NSJSONSerialization JSONObjectWithData:jdata options:kNilOptions error:&error];


附网上查到的读写方法:
将Json存进本地文件夹

NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path=[paths objectAtIndex:0];
    NSString *Json_path=[path stringByAppendingPathComponent:@"JsonFile.json"];
    //==写入文件
    NSLog(@"%@",[JsonData writeToFile:Json_path atomically:YES] ? @"Succeed":@"Failed");
-----------------------

读取Json
//==Json文件路径
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path=[paths objectAtIndex:0];
    NSString *Json_path=[path stringByAppendingPathComponent:@"JsonFile.json"];
    //==Json数据
    NSData *data=[NSData dataWithContentsOfFile:Json_path];
    //==JsonObject

       id JsonObject=[NSJSONSerialization JSONObjectWithData:data
                                                                                                     options:NSJSONReadingAllowFragme nts
                                                                                                         error:&error]
  //nslog (@"%@",jsonobject);打印json字典
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
nlohmann/json库是一个用于处理JSON数据的C++库,它提供了一些常用的方法和技巧。可以通过使用find_package命令引用nlohmann/json库,并在CMake项目中添加库的依赖。具体的方法是使用find_package(nlohmann_json 3.2.0 REQUIRED)命令进行引用,并通过target_link_libraries命令将库链接到你的项目中。 在使用nlohmann-json库时,可以通过定义类型、构造JSON结构以及与结构体关联来实现操作。针对结构体中的每个结构,需要编写一个to_json函数来实现转换。需要注意的是,对于每个自定义的结构,都需要提供to_json函数的实现。 举个例子,你可以使用以下代码来测试nlohmann-json库的读取功能: ```cpp int main(int argc, char **argv) { wb::Person wb = {"wb", 18, true, {"football", "reading"}, {{"house", 9999}, {"car", 8888}}}; if (std::fstream ofs("output.json", std::ios::out); ofs) { json j; wb::to_json(j, wb); ofs << j.dump(4) << '\n'; // 缩进为4的JSON格式 } return 0; } ``` 这段代码创建了一个名为wb的Person结构体对象,并将其转换为JSON格式并保存到output.json文件中。你可以根据需求自行修改代码以适应你的具体情况。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [nlohmann-json库使用简介](https://blog.csdn.net/wbvalid/article/details/119781303)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [C++的nlohmann-json](https://blog.csdn.net/knowledgebao/article/details/122075687)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值