Objective-C Json 使用
2015-07-04 11:55
561 查看
Objective-c json
通过使用NSJSONSerialization 能够Json与Foundation的相互转换。以下详细介绍 Objective-c json 的使用。Json To Fundation
使用 JSONObjectWithData 能够将 Json 转化为 Foundation。Json的顶层能够是{}或
[]因此能够有 NSDictionary 和 NSArray 两种格式。读取使用 ObjectForKey 返回相应的对象。
123456789101112131415161718192021222324252627282930313233 | NSString* items = @"{"items":["item0","item1","item2"]}"; NSData *data= [items dataUsingEncoding:NSUTF8StringEncoding]; NSError *error = nil; id jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error]; if ([jsonObject isKindOfClass:[NSDictionary class]]){ NSDictionary *dictionary = (NSDictionary *)jsonObject; NSLog(@"Dersialized JSON Dictionary = %@", dictionary); }else if ([jsonObject isKindOfClass:[NSArray class]]){ NSArray *nsArray = (NSArray *)jsonObject; NSLog(@"Dersialized JSON Array = %@", nsArray); } else { NSLog(@"An error happened while deserializing the JSON data."); } NSDictionary *dict = (NSDictionary *)jsonObject; NSArray* arr = [dict objectForKey:@"items"];NSLog(@"list is %@",arr); |
Fundation To Json
使用 dataWithJsonObject 能够将 Fundation 转换为 Json。当中 options:NSJSONWritingPrettyPrinted 是分行输出json ,无空格输出使用 option:kNilOptions。以下这段代码是IOS内购获取商品列表。获取后。将内容加入到Json中。
1234567891011121314151617181920212223242526272829303132333435 | NSArray *myProduct = response.products;NSDictionary *myDict;NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity: 4]; for(int i = 0;i<myProduct.count;++i){ //NSLog(@"----------------------"); //NSLog(@"Product title: %@" ,[myProduct[i] localizedTitle]); //NSLog(@"Product description: %@" ,[myProduct[i] localizedDescription]); //NSLog(@"Product price: %@" ,[myProduct[i] price]); //NSLog(@"Product id: %@" ,[myProduct[i] productIdentifier]); myDict = [NSDictionary dictionaryWithObjectsAndKeys: [myProduct[i] localizedTitle], @"title", [myProduct[i] localizedDescription], @"desc", [myProduct[i] price], @"price", [myProduct[i] productIdentifier], @"product", nil]; [dict setValue: myDict forKey: [myProduct[i] productIdentifier]];}if([NSJSONSerialization isValidJSONObject:dict]){ NSError* error; NSData *str = [NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:&error]; NSLog(@"Result: %@",[[NSString alloc]initWithData:str encoding:NSUTF8StringEncoding]);}else{ NSLog(@"An error happened while serializing the JSON data.");} |
相关文章推荐
- Object
- Object-c 中的数据类型
- Objective-C精确的货币计算
- java中顶级父类Object
- 黑马程序员----继承2(接口、多态、object)
- mid range of objective seri
- object-c 之面向对象语法
- Objective-C 中便利初始化方法和指定初始化方法
- Objective-C 快速入门--基础(五)
- Objective-C 快速入门--基础(五)
- Objective-c学习三 控制台字符输入输出
- 正确定义Objective-C常量
- Objective-C(十、NSRange,point,size,rect,NSNumber,NSValue,NSNull)——iOS开发基础
- Objective C多态
- [Objective-C] .m文件不能编译C++代码的问题
- 所闻所获1:动画效果的进度球(水面上升、水面涌动)
- Android 属性动画ObjectAnimator和ValueAnimator讲解
- [学习笔记—Objective-C]《Objective-C 程序设计 第6版》第八章 继承
- [Objective-C] 复写NSObject的description方法
- Objective-C分类 (category)和扩展(Extension)