iOS - OC - JSON 解析 - NSJSONSerialization
#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的更多相关文章
- IOS中Json解析的四种方法
作为一种轻量级的数据交换格式,json正在逐步取代xml,成为网络数据的通用格式. 有的json代码格式比较混乱,可以使用此“http://www.bejson.com/”网站来进行JSON格式化校验 ...
- iOS开源JSON解析库MJExtension
iOS中JSON与NSObject互转有两种方式:1.iOS自带类NSJSONSerialization 2.第三方开源库SBJSON.JSONKit.MJExtension.项目中一直用MJExte ...
- iOS开发-JSON解析
JSON(JavaScript Object Notation)在网络传输中几乎无处不在,JSON是一种轻量级的数据交换格式,是基于JavaScript(Standard ECMA-262 3rd E ...
- 【转】IOS中Json解析的四种方法
原文网址:http://blog.csdn.net/enuola/article/details/7903632 作为一种轻量级的数据交换格式,json正在逐步取代xml,成为网络数据的通用格式. 有 ...
- iOS中JSON解析三方库的比较
网络数据解析框架 1. JsonModel 一个 JSON 模型转换库,有着比较简洁的接口.Model 需要继承自 JSONModel. 2. yyModel yyModel比较轻量(算上.h 只 ...
- iOS 中json解析数据出现中文乱码的问题
一般服务器的编码格式都是UTF8,这样通过json解析下来的的数据,一般中文是不会出现乱码,但是如果服务器的编码格式不是UTF8,通过json解析的数据中的中文容易出现luan乱码,怎么解决这个问题呢 ...
- iOS原生JSON解析.
- (IBAction)accessInterfaceBtnPressed:(id)sender { NSError *error; NSString *URL=@"ht ...
- ios中json解析出现的null问题
http://my.oschina.net/iq19900204/blog/408034 在iOS开发过程中经常需要与服务器进行数据通讯,Json就是一种常用的高效简洁的数据格式. 问题现象 但是几个 ...
- iOS中json解析出现的null,nil,NSNumber的问题
在iOS开发过程中经常需要与服务器进行数据通讯,Json就是一种常用的高效简洁的数据格式. 问题现象 但是几个项目下来一直遇到一个坑爹的问题,程序在获取某些数据之后莫名崩溃.其实很早就发现了原因:由于 ...
随机推荐
- vcenter修改用户密码的方法
https://192.168.x.x:9443登录,必须用administrator@vsphere.local登录,不能用root用户登录. 主页-系统设置- Single Sing-On-用户和 ...
- 学习MongoDB 四: MongoDB查询(一)
一.简介 MongoDB提供了db.collection.find() 方法可以实现根据条件查询和指定使用投影运算符返回的字段省略此参数返回匹配文档中的所有字段. 二.db.collection.fi ...
- 转化Excel表格为php配置文件
<?php //建立reader对象 ,分别用两个不同的类对象读取2007和2003版本的excel文件 require("PHPExcel/Reader/Excel20 ...
- centos7.3安装zip,unzip
安装命令: yum install -y unzip zip
- 《GPU高性能编程CUDA实战》第十一章 多GPU系统的CUDA C
▶ 本章介绍了多设备胸膛下的 CUDA 编程,以及一些特殊存储类型对计算速度的影响 ● 显存和零拷贝内存的拷贝与计算对比 #include <stdio.h> #include " ...
- 0_Simple__template
简单的 CUDA 应用模板,白送的 Sample. ▶ 源代码 //template_cpu.cpp extern "C" void computeGold(float *, co ...
- Nginx相关笔记
相关参考: 编译安装测试nginx https://www.cnblogs.com/jimisun/p/8057156.html
- python读取excel,数字都是浮点型,日期格式是数字的解决办法
excel文件内容: 读取excel: # coding=utf-8 import xlrd import sys reload(sys) sys.setdefaultencoding('utf-8' ...
- 树结构之JavaScript
对于数据结构“树”,想必大家都熟悉,今儿,我们就再来回顾一下数据结构中的二叉树与树,并用JavaScript实现它们. ps:树结构在前端中,很多地方体现得淋漓尽致,如Vue的虚拟DOM以及冒泡等等. ...
- leetcode235
/** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNo ...