JSON的两种常见解析方式
2016-06-27 00:00
447 查看
摘要: 在实际项目开发中后台返回数据绝大多数为JSON数据,对于JSON,首先要明白JSON和XML一样也是一种简单文本格式。相对于XML,它更加易读、更便于肉眼检查。在语法的层面上,JSON与其他格式的区别是在于分隔数据的字符,JSON中的分隔符限于单引号、小括号、中括号、大括号、冒号和逗号。下面介绍NSJSONSerialization解析,JSONKit解析。
#import "ViewController.h"
#import "JSONKit.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *baseString = @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx";
NSURL *baseUrl = [NSURL URLWithString:baseString];
//创建可变的requst对象 设置可变的 要是设置参数;
NSMutableURLRequest *requst = [NSMutableURLRequest requestWithURL:baseUrl];
// 设置Post请求 格式
[requst setHTTPMethod:@"POST"];
// 设置boby参数
NSString *babyString = @"date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213";
NSData *babyData= [babyString dataUsingEncoding:NSUTF8StringEncoding];
[requst setHTTPBody:babyData];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionTask *task = [session dataTaskWithRequest:requst completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
// 系统自带的解析类
[self JSONSystem:data];
// JSON第三方解析
[self JSONThireWay:data];
}];
// 开始网络请求
[task resume];
}
#pragma mark - JSON系统自带的解析类
- (void)JSONSystem:(NSData *)data{
// 获取JOSN数据
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"系统自带解析类获得数据:%@",dict);
}
#pragma mark - JSON第三方JSONKit解析
// 导入#import "JSONKit.h"后会报错,是因为不支持arc 需要在Build Phases->CompileSorces ->双击JSONKit.m,添加-fno-objc-arc
- (void)JSONThireWay:(NSData *)data{
NSDictionary *dict = [data objectFromJSONData];
NSLog(@"第三方解析获得数据:%@",dict);
}
#import "ViewController.h"
#import "JSONKit.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *baseString = @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx";
NSURL *baseUrl = [NSURL URLWithString:baseString];
//创建可变的requst对象 设置可变的 要是设置参数;
NSMutableURLRequest *requst = [NSMutableURLRequest requestWithURL:baseUrl];
// 设置Post请求 格式
[requst setHTTPMethod:@"POST"];
// 设置boby参数
NSString *babyString = @"date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213";
NSData *babyData= [babyString dataUsingEncoding:NSUTF8StringEncoding];
[requst setHTTPBody:babyData];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionTask *task = [session dataTaskWithRequest:requst completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
// 系统自带的解析类
[self JSONSystem:data];
// JSON第三方解析
[self JSONThireWay:data];
}];
// 开始网络请求
[task resume];
}
#pragma mark - JSON系统自带的解析类
- (void)JSONSystem:(NSData *)data{
// 获取JOSN数据
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"系统自带解析类获得数据:%@",dict);
}
#pragma mark - JSON第三方JSONKit解析
// 导入#import "JSONKit.h"后会报错,是因为不支持arc 需要在Build Phases->CompileSorces ->双击JSONKit.m,添加-fno-objc-arc
- (void)JSONThireWay:(NSData *)data{
NSDictionary *dict = [data objectFromJSONData];
NSLog(@"第三方解析获得数据:%@",dict);
}
相关文章推荐
- Call can throw, but it is not marked with 'try'
- Swift-使用NSJSONSerialization生成或解析JSON数据
- OC笔记 - JSON和XML(2015.4.26)
- NSJSONSerialization介绍
- NSJSONSerialization用法
- NSJSONSerialization的使用
- 获取服务器端JSON数据
- NSDictionary转换成json格式的数据
- iOS 【序列化 与 反序列化】
- iOS多线程与网络开发之解析json数据
- 数据解析(三)解析JSON-----系统自带NSJSONSerialization 与 第三方JSONKit
- JSON与XML的区别比较
- IOS中Json解析的四种方法
- Objective-C ,ios,iphone开发基础:JSON解析(使用苹果官方提供的JSON库:NSJSONSerialization)
- 如何在Swift中使用NSError
- NSJSONSerialization 反序列化失败
- NSJSONSerialization
- Swift 解析 JSON
- iOS5自带解析类NSJSONSerialization解析json
- NSJSONSerliazition文档翻译和使用