Objective-C Json 实例详解

所属分类: 软件编程 / IOS 阅读数: 2013
收藏 0 赞 0 分享

Objective-C Json 实例详解

通过使用NSJSONSerialization 可以Json与Foundation的相互转换。下面具体介绍 Objective-c json 的使用。

Json To Fundation

使用 JSONObjectWithData 可以将 Json 转化为 Foundation。Json的顶层可以是{} 或 []因此可以有 NSDictionary 和 NSArray 两种格式。读取使用 ObjectForKey 返回对应的对象。

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中。

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.");
}    

 如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

更多精彩内容其他人还在看

详解优化iOS程序性能的25个方法

本篇文章主要介绍了优化iOS程序性能的25个方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解iOS开发中解析JSON中的boolean类型的数据遇到的问题

这篇文章主要介绍了详解iOS开发中解析JSON中的boolean类型的数据遇到的问题,具有一定的参考价值,有兴趣的可以了解一下。
收藏 0 赞 0 分享

iOS中模态Model视图跳转和Push视图跳转的需求实现方法

这篇文章主要介绍了iOS中模态Model视图跳转和Push视图跳转的需求实现,非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

iOS+PHP注册登录系统 iOS部分(下)

这篇文章主要介绍了iOS+PHP注册登录系统的iOS部分,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

详解iOS - ASIHTTPRequest 网络请求

本篇文章主要介绍了iOS - ASIHTTPRequest 网络请求 ,详细的介绍了 ASIHTTPRequest的使用,具有一定的参考价值,有兴趣的可以了解一下。
收藏 0 赞 0 分享

零基础学习iOS直播之采集

直播的采集由采集的设备(摄像头、话筒)不同分为视频采集和音频采集,本篇文章会分别介绍,需要的朋友一起来看下吧
收藏 0 赞 0 分享

iOS自带动画效果的实例代码

本文给大家分享ios自带动画效果的实现代码,非常不错,具有参考借鉴价值,需要的朋友参考下吧
收藏 0 赞 0 分享

零基础学习iOS直播之播放

对于直播来说,客户端主要做两件事情,推流和播放。本篇主要对播放进行详细介绍,需要的朋友一起来看下吧
收藏 0 赞 0 分享

详解iOS中集成ijkplayer视频直播框架

ijkplayer 是一款做视频直播的框架, 基于ffmpeg, 支持Android和iOS,本文将详细的讲一下在iOS中如何集成ijkplayer, 即便以前从没有接触过,按着下面做也可以集成成功!下面跟着小编一起来看下吧
收藏 0 赞 0 分享

iOS 将系统自带的button改装成上图片下文字的样子

这篇文章主要介绍了 iOS 将系统自带的button改装成上图片下文字的样子,代码是通过继承UIButton,然后再重写layoutSubviews方法,对自带的图片和titleLabel进行重新的layout。下面通过本文给大家分享下实现代码
收藏 0 赞 0 分享
查看更多