当前位置: 首页 > news >正文

Objective-c把字符解析成字典

解析JSON字符串为字典

在Objective-C中,将JSON字符串解析为字典可以使用NSJSONSerialization类。该方法适用于标准的JSON格式字符串。

NSString *jsonString = @"{\"name\":\"John\", \"age\":30}";
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];NSError *error;
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];if (error) {NSLog(@"解析失败: %@", error.localizedDescription);
} else {NSLog(@"解析结果: %@", dictionary);
}

解析URL查询字符串为字典

对于URL查询字符串(如"key1=value1&key2=value2"),可以使用以下方法转换为字典:

NSString *queryString = @"name=John&age=30";
NSMutableDictionary *params = [NSMutableDictionary dictionary];NSArray *pairs = [queryString componentsSeparatedByString:@"&"];
for (NSString *pair in pairs) {NSArray *elements = [pair componentsSeparatedByString:@"="];if (elements.count == 2) {NSString *key = [elements[0] stringByRemovingPercentEncoding];NSString *value = [elements[1] stringByRemovingPercentEncoding];params[key] = value;}
}NSLog(@"解析结果: %@", params);

解析属性列表(plist)字符串为字典

对于属性列表格式的字符串,可以使用NSPropertyListSerialization

NSString *plistString = @"<dict><key>name</key><string>John</string><key>age</key><integer>30</integer></dict>";
NSData *plistData = [plistString dataUsingEncoding:NSUTF8StringEncoding];NSError *error;
NSDictionary *dictionary = [NSPropertyListSerialization propertyListWithData:plistData options:NSPropertyListImmutable format:NULL error:&error];if (error) {NSLog(@"解析失败: %@", error.localizedDescription);
} else {NSLog(@"解析结果: %@", dictionary);
}

解析自定义格式字符串为字典

对于自定义格式的字符串,可能需要编写特定的解析逻辑:

NSString *customString = @"name:John,age:30,city:New York";
NSMutableDictionary *resultDict = [NSMutableDictionary dictionary];NSArray *components = [customString componentsSeparatedByString:@","];
for (NSString *component in components) {NSArray *keyValue = [component componentsSeparatedByString:@":"];if (keyValue.count == 2) {NSString *key = [keyValue[0] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];NSString *value = [keyValue[1] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];resultDict[key] = value;}
}NSLog(@"解析结果: %@", resultDict);

注意事项

处理字符解析时需要考虑字符串编码问题,通常使用UTF-8编码。解析过程中应添加错误处理机制,捕获可能出现的异常情况。对于复杂或嵌套的数据结构,可能需要递归解析方法。

http://www.lryc.cn/news/577179.html

相关文章:

  • python包管理工具uv VS pip
  • 在Flutter中生成App Bundle并上架Google Play
  • camera调试:安卓添加xml注册
  • 二刷 苍穹外卖day09
  • 【硬核数学 · LLM篇】3.1 Transformer之心:自注意力机制的线性代数解构《从零构建机器学习、深度学习到LLM的数学认知》
  • 借助 Wisdom SSH,实现 Linux 用户组与权限的精细化智能管控
  • DataGrip测试连接时出现报错解决方案
  • 谷歌高调宣布,Gemini 2.5 Pro正式版,绘制常见图表(折线图、柱状图、PPT等),国内直接使用
  • 将 h264+g711a存为 mp4文件,记录
  • uniapp+vue2 ba-tree-picker下拉项多选 树形层级选择器(支持单选、多选、父级选择、映射)
  • SAP月结问题9-FAGLL03H与损益表中研发费用金额不一致(FAGLL03H Bug)
  • 【数据结构中的位运算】
  • 堆排序实现及复杂度分析
  • AWS WebRTC:通过shell分析并发启动master后产生的日志文件
  • 腾讯云空间,高性能显卡云,安装xinference报错,pip install 空间不够用了
  • 大语言模型(LLM)笔记
  • JavaEE-MyBatis-Plus
  • datax-web报错:连接数据库失败. 请检查您的 账号、密码、数据库名称、IP、Port或者向 DBA 寻求帮助(注意网络环境)
  • Flutter插件ios_pod
  • 跨时间潜运动迁移以实现操作中的多帧预测
  • 云效DevOps vs Gitee vs 自建GitLab的技术选型
  • 临床试验审计问题分类与整改策略
  • 高效数据采集:Python与Rust完美结合
  • 将本地仓库推送到GitHub
  • 【Pandas】pandas DataFrame attrs
  • 2025年光学工程、精密仪器与光电子技术国际会议(OEPIOT 2025)
  • 【MCP服务】蓝耘元生代 | 蓝耘MCP平台来袭!DeepSeek MCP服务器玩转大模型集成
  • Python-Word文档、PPT、PDF以及Pillow处理图像详解
  • 车载ECU刷写文件格式汇总详解
  • 博图SCL编程:结构体(STRUCT)使用详解与实战案例