ios5 支持 json 转化
2012-08-17 17:00
197 查看
解析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] |
相关文章推荐
- JSON相关的,数据彼此间的转化进行了简单地封装,源码如下,支持arc与非arc
- iOS Json和对象互转(反射支持对象嵌套的复杂对象类型,支持基本数据类型和NSDate属性转化)
- IOS中四种解析JSON的方法(IOS5自带支持方法、JSONKit、SBJson、TouchJson)
- java转化json方式2(需要json官方jar包支持)
- java转化json方式2,(需要json官方jar包支持)
- iOS5系统API和5个开源库的JSON解析速度测试
- ADO.NET Data Service如何直接支持用Json格式返回数据
- 在Struts 2中使用JSON Ajax支持
- 彻底告别等待越狱!不越狱安装下载的IPA,iPhone 4S iOS5已测!!希望大家支持!
- 将实体转化成json并将json转换成DataTable
- PHP5.1不支持json_encode的php实现
- Ajax_基础知识_使用Jackson第三方工具包轻松转化成JSON字符串
- Json转化
- json字符串和json对象相互转化
- C# ASP.NET替换要转化为json的数据中的特殊符号
- Json转换利器Gson之实例一-简单对象转化和带泛型的List转化
- Spring MVC关于IE对application/json的content-type不支持解决方案---duang 解决了
- 字符串转化为Json格式的数据--(记录四)
- 如何将JSON字符创转化成JSON对象
- 前台序列化传过来的值,后台获取之后封装到map当中,让后在转化成json格式,最后在把json里面的参数里面的某一个值进行分割,最后在存到json格式的数据中去。