当前位置: 首页 > 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.dtcms.com/a/262917.html

相关文章:

  • 【微服务】.Net中使用Consul实现服务高可用
  • 链表重排序问题
  • java JNDI高版本绕过 工具介绍 自动化bypass
  • Python训练营打卡Day58(2025.6.30)
  • 晨控CK-FR03与和利时LX系列PLC配置EtherNetIP通讯连接操作手册
  • linux下fabric环境搭建
  • [免费]微信小程序停车场预约管理系统(Springboot后端+Vue3管理端)【论文+源码+SQL脚本】
  • Spring Security 鉴权与授权详解(前后端分离项目)
  • 系统自带激活管理脚本 ospp.vbs 文件
  • Python 的内置函数 object
  • Spring Boot属性配置方式
  • Linux 系统管理:自动化运维与容器化部署
  • 淘宝API接口在数据分析中的应用
  • 【Day 7-N17】Python函数(1)——函数定义、位置参数调用函数、关键字参数调用函数、函数的默认值
  • JMeter常用断言方式
  • python crawling data
  • HTML5 实现的圣诞主题网站源码,使用了 HTML5 和 CSS3 技术,界面美观、节日氛围浓厚。
  • VR协作香港:虚拟现实技术重塑商业协作新模式
  • Jenkins Pipeline 实战指南
  • VMware vSphere 9与ESXi 9正式发布:云原生与AI驱动的虚拟化平台革新
  • Oracle 树形统计再进阶:类型多样性与高频类型分析(第三课)
  • 【无标题】LandingAi使用
  • 腾讯云实名资质 “待补充后提交” 解决方法
  • MIT 6.824学习心得(2) 浅谈多线程和RPC
  • NLP自然语言处理 01 文本预处理
  • ChatGPT、DeepSeek等大语言模型技术教程
  • (二十一)-java+ selenium-浏览器窗口句柄用法
  • 华为云Flexus+DeepSeek征文|基于 Dify-LLM 构建网站智能客服助手的实践探索
  • Adobe付费AI功能实用技巧与设计师创新思维分享
  • 青少年编程与数学 02-022 专业应用软件简介 03 三维建模及动画软件:Autodesk Maya