iOS 读取本地 JSON 文件

对于有些数据 是死数据,不需要每次都从服务器上拉取,就可以以JSON 方式保存在本地。

代码示例如下:

// 读取本地JSON文件
- (NSArray *)readLocalFileWithName:(NSString *)name {
    // 获取文件路径
    NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"json"];
    // 将文件数据化
    NSData *data = [[NSData alloc] initWithContentsOfFile:path];
    // 对数据进行JSON格式化并返回字典形式
    return [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
}

这里返回的是NSArray ,你也可以返回 NSDictionary。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C++中将Labelme JSON文件中的imagedata转换为JPG格式的图片,可以按照以下步骤进行: 1. 打开Labelme的JSON文件,找到imagedata所在的字段。 2. 将imagedata字段中的Base64编码数据解码为二进制数据。 3. 将二进制数据保存为JPG格式的图片文件。 具体的C++代码示例如下: ```c++ #include <fstream> #include <iostream> #include <string> #include <vector> #include <opencv2/opencv.hpp> #include <boost/algorithm/string.hpp> #include <boost/algorithm/string/replace.hpp> using namespace std; using namespace cv; int main() { // 读取JSON文件 ifstream ifs("example.json", ios::in); string json_str((istreambuf_iterator<char>(ifs)), istreambuf_iterator<char>()); ifs.close(); // 找到imagedata字段 string imagedata_start = "\"imageData\":\""; string imagedata_end = "\",\"shapes\":"; size_t pos1 = json_str.find(imagedata_start); size_t pos2 = json_str.find(imagedata_end); string imagedata = json_str.substr(pos1 + imagedata_start.length(), pos2 - pos1 - imagedata_start.length()); // 替换Base64编码中的特殊字符 boost::replace_all(imagedata, "-", "+"); boost::replace_all(imagedata, "_", "/"); int padding = 4 - imagedata.length() % 4; if (padding < 4) imagedata.append(padding, '='); // 解码Base64编码 vector<unsigned char> im_bytes = base64_decode(imagedata); // 将二进制数据转换为OpenCV图像对象 Mat im = imdecode(im_bytes, IMREAD_COLOR); // 保存为JPG格式的图片文件 imwrite("example.jpg", im); return 0; } ``` 在上面的代码中,我们首先读取JSON文件,并找到了imagedata字段中的Base64编码数据。然后,我们使用Boost库将Base64编码中的特殊字符替换为标准的Base64字符,并解码为二进制数据。接下来,我们使用OpenCV库将二进制数据转换为图像对象,并保存为JPG格式的图片文件

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值