IOS中使用json
来源:互联网 发布:手机陀螺仪校准软件 编辑:程序博客网 时间:2024/06/12 13:13
1、从https://github.com/stig/json-framework/中下载json框架:json-framework
2、解压下载的包,将class文件夹下的所有文件导入到当前工程下。
3、在使用的文件中加入导入语句 :#import "SBJson.h"
4、将json字符串转为NSDictionary对象
//测试json的解析-(void)testJsonParser: (NSString *) jsonString{ jsonString = [[NSString alloc] initWithString:@"{\"userInfo\":{\"userName\":\"张三\",\"sex\":\"男\"}}"]; NSLog(@"正在解析json字符串是:%@",jsonString); SBJsonParser * parser = [[SBJsonParser alloc] init]; NSError * error = nil; NSMutableDictionary *jsonDic = [parser objectWithString:jsonString error:&error]; NSMutableDictionary * dicUserInfo = [jsonDic objectForKey:@"userInfo"]; NSLog(@"%@",[jsonDic objectForKey:@"userInfo" ]); NSLog(@"%@",[dicUserInfo objectForKey:@"userName"]); NSLog(@"%@",[dicUserInfo objectForKey:@"sex"]);}
5、处理json对象有多个记录的方法
NSString * customerGridJsonString = [[NSString alloc]initWithString:@"{\"customer\":[{\"name\":\"roamer\",\"ycount\":\"232.4\",\"sumcount\":\"322.3\"},{\"name\":\"王三\",\"ycount\":\"221.2\",\"sumcount\":\"1123.2\"},{\"name\":\"李四\",\"ycount\":\"1221.2\",\"sumcount\":\"12123.2\"}]}"]; SBJsonParser * parser = [[SBJsonParser alloc] init];// NSLog(@"%@",customerGridJsonString); NSError * error = nil; NSMutableDictionary *root = [[NSMutableDictionary alloc] initWithDictionary:[parser objectWithString:customerGridJsonString error:&error]]; NSLog(@"%@",root); //注意转换代码 SBJsonWriter *jsonWriter = [[SBJsonWriter alloc] init]; NSString *jsonString = [jsonWriter stringWithObject:root]; [jsonWriter release]; NSLog(@"%@",jsonString); //注意转换代码 NSMutableArray * customers = [root objectForKey:@"customer"]; NSLog(@"%@",customers); for(NSMutableDictionary * member in customers) { NSLog(@"%@",[[member objectForKey:@"name"] description]); }
6、递归遍历解析出的NSDictionary对象
-(void)visitDict:(NSDictionary *)dict{ NSArray *keys=[dict allKeys]; for (NSString *key in keys) { NSString *result=[NSString stringWithFormat:@"key=%@,value=%@",key,[dict objectForKey:key]]; NSLog(result); if([[dict objectForKey:key] isKindOfClass:[NSDictionary class]]){ [self visitDict:[dict objectForKey:key]]; } } }
7、将解析出的NSDictionary对象还原为json字符串
NSString * jsonStr=[items JSONRepresentation];
- IOS中使用json
- ios 使用json
- ios 中使用SBJson拼接和解析json
- ios中使用 NSJSONSerialization对 json数据的解析
- ios 中使用SBJson拼接和解析json
- iOS-OC中使用KVC转换JSON数据(笔记)
- ios中JSON的解析
- iOS中model 转json
- iOS中JSON数据解析
- iOS 中json的解析
- IOS中JSON数据解析
- iOS 中json数据用法
- iOS中 字典转Json
- iOS中解析json数据
- iOS中JSON数据解析
- Flex中使用Json
- Ajax中使用JSON
- Struts2中使用JSON
- 使用SeaJS实现模块化JavaScript开发
- 不定输入参数函数的编程中需要用到的宏,即注意事项
- C++引用与指针的区别
- NPN和PNP作为开关管的设计技巧以及全系列三极管参数
- Linux/Ubuntu下 静态编译Qt程序
- IOS中使用json
- 黑客帝国并非虚构,世界就是程序代码,人类末日近在咫尺(连载)
- 安装CRM2011需要注意事项
- 一些常见svn图标的含义
- db2常用命令大全
- Android应用开发实战GPS与加速度传感器
- 自定义SqlMembershipProvider方法
- cocos2d中CCCallFunc、CCCallFuncN和CCCallFuncND的区别
- 在hadoop框架中编译执行程序