/*

      总结:   json格式的读写:

解析:

data =   NSData  dataWithContentsOfUrl:XXX

id obj  =  [ NSJsonSerialization NSJSONObjectWithData:data XXXX];

生成json格式的文件

array或 dict

NSData* data  = [NSJSONSerialization dataWithJSONObject:array/dict XXXX];

data writeToFile:PATH;

或者

data writeToURL:(NSURL*)url;

*/

#define urlString @"http://localhost/files/test.json"

#define cpuString @"http://localhost/files/cpu.json"

void parseJsonData(void)

{

//将特殊字符编码,例如 中文

//    urlString stringByAddingPercentEscapesUsingEncoding:<#(NSStringEncoding)#>

//将一些被编码的字符串解码成特殊字符中

//    urlString stringByReplacingPercentEscapesUsingEncoding:<#(NSStringEncoding)#>

//将url字符串编码,即去除特殊字符串,若是没有特殊字符,可以不必

NSString* validUrlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSURL * url = [NSURL URLWithString:validUrlString ];

NSData* data = [ NSData dataWithContentsOfURL:url ];

NSString *contents = [[ NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];

NSLog(@"contents:%@",contents);

NSError* error=nil;

NSArray* array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];

}

//********************************************

#define PATH @"/Users/wenhua/Desktop/write.json"

//把内存中的数据构造成json格式的字节流

void makeJsonData(void)

{

NSMutableArray *array = [[ NSMutableArray alloc]init];

[array addObject:@"abc"];

[array addObject:[NSNumber numberWithInt:123]];

[array addObject:[NSNumber numberWithBool:YES]];

[array addObject:[NSNumber numberWithFloat:3.14159]];

[array addObject:[NSNull null]];

//创建一个字典

NSDictionary* dict=@{@"k1":@"v1",@"k2":@"v2"};

[array addObject:dict];

NSArray* arr = @[@"one",@"two",@"three" ];

[array addObject:arr];

NSError* error=nil;

NSData* data = [NSJSONSerialization dataWithJSONObject:array options:NSJSONWritingPrettyPrinted error:&error];

if (error) {

NSLog(@"%@",error);

return;

}

[data writeToFile:PATH  atomically:YES];

}

NSJSONSerialization的更多相关文章

  1. iOS json解析的几种方法 NSJSONSerialization,JSONKit,SBJson ,TouchJson

    相关的第三方类库大家可以去github上下载 1.NSJSONSerialization 具体代码如下 : - (void)viewDidLoad { [super viewDidLoad]; NSD ...

  2. NSJSONSerialization(json序列化)

    //通过调用isValidJSONObject来判断Foundation对象是否可以转换为JSON数据 NSJSONSerialization isValidJSONObject:obj 我们能利用N ...

  3. 使用NSJSONSerialization将数组或字典转为字符串

    IOS中将数组或字典转为字符串可以用NSJSONSerialization,代码如下: NSData* data = [NSJSONSerialization dataWithJSONObject:a ...

  4. NSJSONSerialization 组json字符串

    抄的网上的. 主要是组织列表部分 NSDictionary *song = [NSDictionary dictionaryWithObjectsAndKeys:",@"lengt ...

  5. iOS开发——网络篇——JSON和XML,NSJSONSerialization ,NSXMLParser(XML解析器),NSXMLParserDelegate,MJExtension (字典转模型),GDataXML(三方框架解析XML)

    一.JSON 1.JSON简介什么是JSONJSON是一种轻量级的数据格式,一般用于数据交互服务器返回给客户端的数据,一般都是JSON格式或者XML格式(文件下载除外) JSON的格式很像OC中的字典 ...

  6. 系统自带的NSJSONSerialization解析json文件

    #import "ViewController.h" #import "Student.h" #import "GDataXMLNode.h" ...

  7. iOS下json的解析 NSJSONSerialization

      - (IBAction)JOSNButtonPressed:(id)sender { NSString *str=[@"http://douban.fm/j/mine/playlist? ...

  8. NSJSONSerialization介绍

      ios5中apple增加了解析JSON的api——NSJSONSerialization.网上已经有人做过测试,NSJSONSerialization在效率上完胜SBJSON.TouchJSON. ...

  9. Objective-C ,ios,iphone开发基础:JSON解析(使用苹果官方提供的JSON库:NSJSONSerialization)

    json和xml的普及个人觉得是为了简化阅读难度,以及减轻网络负荷,json和xml 数据格式在格式化以后都是一种树状结构,可以树藤摸瓜的得到你想要的任何果子. 而不格式化的时候json和xml 又是 ...

随机推荐

  1. [Core] .NET Core & VS Code 之路(1) Hello World

    目录 相关链接 dotnet命令 VS Code Hello World Web Hello World 总结 其实本篇上个月已经写好, 只是 但是,不忘初心方得始终 相关链接 Learn .NET ...

  2. 【转载】CSS Sticky Footer: 完美的CSS绝对底部

    下面是我找到的一个比较完美的方法,来自国外的设计达人,纯CSS,可以实现: 当正文内容很少时,底部位于窗口最下面.当改变窗口高度时,不会出现重叠问题. <div id="wrap&qu ...

  3. C# 日志的配置流程

    1. 下载log4net.dll文件 http://download.csdn.net/detail/abc456456456456/7653857 2. 项目中引用此dll 3. appconfig ...

  4. C# 异步工具类 及一点小小的重构经验

    2015年新年第一篇随笔, 祝福虽然有些晚,但诚意还在:新年快乐. 今天主要是想分享一异步工具类,在C/S架构中.先进行网络资源异步访问,然后将回调函数 Invoke到UI线程中进行UI处理. 这样的 ...

  5. Swift注释

    Swift 中的普通注释与 Objective-C 中的一致,大概分为以下三种 // 单行注释 何问起 / *  多行注释 hovertree.com */ /// 标记注释1 http://hove ...

  6. Guava并发:ListenableFuture与RateLimiter示例

    ListenableFuture顾名思义就是可以监听的Future,它是对java原生Future的扩展增强 RateLimiter类似于JDK的信号量Semphore,他用来限制对资源并发访问的线程 ...

  7. Web.config配置文件详解

    整理了一下ASP.NET Web.config配置文件的基本使用方法.很适合新手参看,由于Web.config在使用很灵活,可以自定义一些节点.所以这里只介绍一些比较常用的节点. <?xml v ...

  8. 深度技术32位Win7系统Ghost版

    深度技术32位Win7系统Ghost版,GhostWin7是指使用Ghost软件做成压缩包的Windows7,俗称克隆版Win7.用克隆版的目的是节省安装时间.本作品在采用微软封装部署技术的基础上,结 ...

  9. 泛函编程(32)-泛函IO:IO Monad

    由于泛函编程非常重视函数组合(function composition),任何带有副作用(side effect)的函数都无法实现函数组合,所以必须把包含外界影响(effectful)副作用不纯代码( ...

  10. pm2 配置

    ---恢复内容开始--- 1. ecosystem.json { "apps": [ { "name": "name", // 项目名 &q ...