#import "ViewController.h"

 @interface ViewController ()

 @end

 @implementation ViewController

 -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self test];
} -(void)jsonToOC
{
//1.确定url
NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login?username=123&pwd=456&type=JSON"]; //2.创建请求对象
NSURLRequest *request = [NSURLRequest requestWithURL:url]; //3.发送异步请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
//data---->本质上是一个json字符串
//4.解析数据
//NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]); //JSON--->oc对象 反序列化
/*
第一个参数:JSON的二进制数据
第二个参数:
第三个参数:错误信息
*/
/*
NSJSONReadingMutableContainers = (1UL << 0), 可变字典和数组
NSJSONReadingMutableLeaves = (1UL << 1), 内部所有的字符串都是可变的 ios7之后又问题 一般不用
NSJSONReadingAllowFragments = (1UL << 2) 既不是字典也不是数组,则必须使用该枚举值
*/ NSString *strM = @"\"wendingding\""; // NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; id obj = [NSJSONSerialization JSONObjectWithData:[strM dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:nil]; NSLog(@"%@---%@",[obj class],obj); }]; } //JSON--->OC
-(void)JSONWithOc
{
//NSString *strM = @"{\"error\":\"用户名不存在\"}";
//NSString *strM = @"[\"error\",\"用户名不存在\"]";
//NSString *strM = @"\"wendingding\"";
//NSString *strM = @"false";
//NSString *strM = @"true";
NSString *strM = @"null"; id obj = [NSJSONSerialization JSONObjectWithData:[strM dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:];
NSLog(@"%@---%@",[obj class],obj); /*
JOSN OC
{} @{}
[] @[]
"" @""
false NSNumber 0
true NSNumber 1
null NSNull为空
*/ //nil
[NSNull null]; //该方法获得的是一个单粒,表示为空,可以用在字典或者是数组中 } //OC--->json
-(void)OCtojson
{
NSDictionary *dictM = @{
@"name":@"dasheng11",
@"age":@
}; NSArray *arrayM = @[@"",@""]; //注意:并不是所有的OC对象都能转换为JSON
/*
- 最外层必须是 NSArray or NSDictionary
- 所有的元素必须是 NSString, NSNumber, NSArray, NSDictionary, or NSNull
- 字典中所有的key都必须是 NSStrings类型的
- NSNumbers不能死无穷大
*/
NSString *strM = @"WENIDNGDING"; BOOL isValid = [NSJSONSerialization isValidJSONObject:strM];
if (!isValid) {
NSLog(@"%zd",isValid);
return;
} //OC--->json
/*
第一个参数:要转换的OC对象
第二个参数:选项NSJSONWritingPrettyPrinted 排版 美观
*/
NSData *data = [NSJSONSerialization dataWithJSONObject:strM options:NSJSONWritingPrettyPrinted error:nil]; NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
} -(void)test
{
NSArray *arrayM = [NSArray arrayWithContentsOfFile:@"/Users/xiaomage/Desktop/课堂共享/11大神班上课资料/05-多线程网络/0225/资料/apps.plist"];
NSLog(@"%@",arrayM); //[arrayM writeToFile:@"/Users/xiaomage/Desktop/123.json" atomically:YES]; //OC--->JSON
NSData *data = [NSJSONSerialization dataWithJSONObject:arrayM options:NSJSONWritingPrettyPrinted error:];
[data writeToFile:@"/Users/xiaomage/Desktop/123.json" atomically:YES];
}

iOS - OC - JSON 解析 - NSJSONSerialization的更多相关文章

  1. IOS中Json解析的四种方法

    作为一种轻量级的数据交换格式,json正在逐步取代xml,成为网络数据的通用格式. 有的json代码格式比较混乱,可以使用此“http://www.bejson.com/”网站来进行JSON格式化校验 ...

  2. iOS开源JSON解析库MJExtension

    iOS中JSON与NSObject互转有两种方式:1.iOS自带类NSJSONSerialization 2.第三方开源库SBJSON.JSONKit.MJExtension.项目中一直用MJExte ...

  3. iOS开发-JSON解析

    JSON(JavaScript Object Notation)在网络传输中几乎无处不在,JSON是一种轻量级的数据交换格式,是基于JavaScript(Standard ECMA-262 3rd E ...

  4. 【转】IOS中Json解析的四种方法

    原文网址:http://blog.csdn.net/enuola/article/details/7903632 作为一种轻量级的数据交换格式,json正在逐步取代xml,成为网络数据的通用格式. 有 ...

  5. iOS中JSON解析三方库的比较

    网络数据解析框架 1.  JsonModel 一个 JSON 模型转换库,有着比较简洁的接口.Model 需要继承自 JSONModel. 2.  yyModel yyModel比较轻量(算上.h 只 ...

  6. iOS 中json解析数据出现中文乱码的问题

    一般服务器的编码格式都是UTF8,这样通过json解析下来的的数据,一般中文是不会出现乱码,但是如果服务器的编码格式不是UTF8,通过json解析的数据中的中文容易出现luan乱码,怎么解决这个问题呢 ...

  7. iOS原生JSON解析.

    - (IBAction)accessInterfaceBtnPressed:(id)sender {        NSError *error;    NSString *URL=@"ht ...

  8. ios中json解析出现的null问题

    http://my.oschina.net/iq19900204/blog/408034 在iOS开发过程中经常需要与服务器进行数据通讯,Json就是一种常用的高效简洁的数据格式. 问题现象 但是几个 ...

  9. iOS中json解析出现的null,nil,NSNumber的问题

    在iOS开发过程中经常需要与服务器进行数据通讯,Json就是一种常用的高效简洁的数据格式. 问题现象 但是几个项目下来一直遇到一个坑爹的问题,程序在获取某些数据之后莫名崩溃.其实很早就发现了原因:由于 ...

随机推荐

  1. [UE4]崩溃的原因收录

    UTool tool; 这样声明可以编译通过,但是UE4 Editor会直接崩溃. 应该改成这样: UTool* tool;

  2. 【C++11新特性】 auto关键字

    原文链接: http://blog.csdn.net/xiejingfa/article/details/50469045 熟悉脚本语言的人都知道,很多脚本语言都引入了“类型自动推断”技术:比如pyt ...

  3. php如何高效的读取大文件

    通常来说在php读取大文件的时候,我们采用的方法一般是一行行来讲取,而不是一次性把文件全部写入内存中,这样会导致php程序卡死,下面就给大家介绍这样一个例子. 需求:有一个800M的日志文件,大约有5 ...

  4. smb.conf详解[未完]

    看着玩意看的吐血!!!! baidu\google充斥着一堆错误的文章及翻译,samba.org上动辄就是this document is old and might be incurrent. 不过 ...

  5. 启动 node 文件时附带参数

    cmd: node app.js hello app.js var args = process.argv; console.log(args);//[ 'C:\\Program Files\\nod ...

  6. Python之函数——进阶篇

    嵌套函数 ---函数内部可以再次定义函数 ---函数若想执行,必须被调用 注意,下例中,执行结果为什么? age = 19 def func1(): print(age) def func2(): p ...

  7. vb 读取指定路径文件名

    Private Sub ExportCostSheetData() InsertRow("") InsertRow("Run 2:Export CostingSheet= ...

  8. ELK配置过程初次安装使用心得--elasticsearch5.4版--及logstash

    安装所遇到的问题:http://www.bubuko.com/infodetail-1889252.html 一,先创建用户和组groupadd es useradd -g es es passwd  ...

  9. JAVA 文件与base64之间的转化, 以及Web实现base64上传文件

    <1>文件与base64字符串之间的转化 package servlet_file_upload; import java.io.File; import java.io.FileInpu ...

  10. C# ADO.NET 封装的增删改查

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...