IOS5.0 自带JSON解析
来源:互联网 发布:如何在页面注入js脚本 编辑:程序博客网 时间:2024/05/22 03:16
IOS5.0开始支持JSON,所以不用第三方的解析了。
//将Foundation(NSDictionary、NSData)转换为JSon格式的NSData用来发送
//+ dataWithJSONObject:options:error:
//将JSON格式的Data转换为Foundation(NSDictionary、NSData) 用来解析
//+ JSONObjectWithData:options:error:
code:
https://github.com/cokecoffe/ios-demo/tree/master/HTTP
- 解析json成dic对象
- -(void)fetchedData:(NSData*)responseData {//parse out the json dataNSError* error;
- NSDictionary* json =[NSJSONSerialization
- JSONObjectWithData:responseData //1
- options:kNilOptions
- error:&error];
- NSArray* latestLoans =[json objectForKey:@"loans"];//2
- NSLog(@"loans: %@", latestLoans);//3
- }
- 把对象生成json string
- //build an info object and convert to json
- NSDictionary* info =[NSDictionary dictionaryWithObjectsAndKeys:[loan objectForKey:@"name"],
- @"who",
- [(NSDictionary*)[loan objectForKey:@"location"]
- objectForKey:@"country"],
- @"where",
- [NSNumber numberWithFloat: outstandingAmount],
- @"what",
- nil];
- //convert object to data
- NSData* jsonData =[NSJSONSerialization dataWithJSONObject:info
- options:NSJSONWritingPrettyPrinted error:&error];
- //print out the data contents
- jsonSummary.text =[[NSString alloc] initWithData:jsonData
- encoding:NSUTF8StringEncoding];
- 添加json方法至dic
- @interfaceNSDictionary(JSONCategories)
- +(NSDictionary*)dictionaryWithContentsOfJSONURLString:(NSString*)urlAddress;
- -(NSData*)toJSON;
- @end
- @implementationNSDictionary(JSONCategories)
- +(NSDictionary*)dictionaryWithContentsOfJSONURLString:(NSString*)urlAddress{
- NSData* data =[NSData dataWithContentsOfURL:[NSURL URLWithString: urlAddress]];
- __autoreleasing NSError* error =nil;
- id result =[NSJSONSerialization JSONObjectWithData:data
- options:kNilOptions error:&error];
- if(error !=nil)returnnil;
- return result;
- }
- -(NSData*)toJSON{
- NSError* error =nil;
- id result =[NSJSONSerialization dataWithJSONObject:self
- options:kNilOptions error:&error];
- if(error !=nil)returnnil;
- return result;
- }@end
- 使用列子
- NSDictionary* myInfo =[NSDictionary dictionaryWithContentsOfJSONURLString:@"http://www.yahoo.com/news.json"];
- NSDictionary* information =[NSDictionary dictionaryWithObjectsAndKeys:@"orange",@"apple",@"banana",@"fig",nil];
- NSData* json =[information toJSON];
- 判断是否可json化
- BOOL isTurnableToJSON =[NSJSONSerialization isValidJSONObject: object]
- IOS5.0 自带JSON解析
- iOS5自带解析类NSJSONSerialization解析json
- IOS中四种解析JSON的方法(IOS5自带支持方法、JSONKit、SBJson、TouchJson)
- 自带JSON解析工具
- json 利用自带json解析
- 发送请求JSON解析自带
- NSJSONSerialization iOS自带解析json
- iOS 自带方法解析json
- json解析之自带方法
- 使用as3.0自带JSON解析工具解析JSON数据
- 【Android】解析Json对象,用自带json解析器
- IOS自带json解析之生成json对象
- iOS5.0新功能之——NSJSONSerialization解析JSON数据
- iOS5.0新功能之——NSJSONSerialization解析JSON数据
- iOS5.0新功能之——NSJSONSerialization解析JSON数据
- iOS5.0新功能之——NSJSONSerialization解析JSON数据
- Android使用自带JSONObject解析JSON数据
- JSON复杂文档解析 Android自带JSONObject,JSONArray方法
- 科技写作漫谈 (29):学术讲演与PPT制作
- ARM Assembly Language Programming (part 7)
- PowerDesigner中CDM和PDM数据类型另CDM图介绍
- KindEditor 编辑器使用方法
- Eclipse 安装插件
- IOS5.0 自带JSON解析
- poj Hardwood Species
- 转 图解集成 Google Code 到 Eclipse Mylyn
- 程序异常退出时发送邮件报告的代码
- 枚举
- jquery ajax struts2用户名验证(get)
- socket正确关闭
- 我所积累的20条编程经验
- VM9.0 fedora10.0下安装配置arm-linux-gcc-4.3.2