#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. 检测2个公网IP的GRE隧道是否通的方法,使用PPTP拨号检测。

    检测2个公网IP的GRE隧道是否通的方法,使用PPTP拨号检测. 因为PPTP是建立在GRE隧道基础上的. PPTP 防火墙开放 TCP 1723防火墙开放 IP protocol 47,即GRENA ...

  2. thinkphp 5.0手记

    场景配置,可配置多个数据库,按需求加载 数组合并:array_merge();键名相同后面覆盖前面 array_merge_recursive();键名相同,键值合并 对与http://localho ...

  3. Git强制拉取覆盖本地 Pull force

    git fetch --all git reset --hard origin/master git pull 单条执行 git fetch --all && git reset -- ...

  4. XPath 常用语法札记

    * 不包含属性的元素 例如不包含属性的span: span[not(@*)] * 文本包含某部分的元素 例如文本包含Rank的元素: *[contains(text(),'Rank')] * 选择匹配 ...

  5. 红帽yum源安装报错initscripts-9.49.41-1.el7.x86_64 conflicts redhat-release &lt; 7.5-0.11" ?

    https://access.redhat.com/solutions/3425111 环境 Red Hat Enterprise Linux 7 问题 yum fails to apply upda ...

  6. zabbix监控系统日志

    监控日志必须让agent运行在主动模式 参考网站:https://www.cnblogs.com/dadonggg/p/8611054.html?from=singlemessage

  7. Axon框架使用指南

    参考:https://blog.csdn.net/wangli13860426642/article/details/80018222 详细参考:   https://blog.csdn.net/wa ...

  8. jpa-jpql-basic-test

    jpql 基本测试 //可以使用 JPQL 完成 UPDATE 和 DELETE 操作. @Test public void testExecuteUpdate(){ String jpql = &q ...

  9. icil 参考docker

    @All 有不知道怎么用docker发布项目的,请参考 http://192.168.18.224:8888/svn/Enterprise/site/docker/overview of docker ...

  10. 【Javascript Demo】遮罩层和百度地图弹出层简单实现

    其实想做的就是显示百度地图的弹出层,现在已经简单实现了.示例和代码如下,点击按钮可以看到效果: 1.示例:   2.代码: <!DOCTYPE html PUBLIC "-//W3C/ ...