iOS-jason解析的方法:解析库

在做项目的时候免不了要解析网络数据,XML 和 JSon 格式

这篇文章探讨解析JSon数据:

JSon解析库:

1、NSJSONSerialization (苹果 PAI)

2、JSONKit 

3、NextiveJson

4、YAJL

5、SBJSON  (json-framework)

6、TouchJSON

速度比较:

大部分人用SBJSon,但是5、6的速度最慢,  1、2的速度最快。

使用详解:

1、NSJSONSerialization

https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSJSONSerialization_Class/index.html

解析json数据为 NSDictionary、NSArray :

NSData *data = dataes;//网络解析到dataes
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
//再从字典中获取各种数据

封装 NSDictionary 、 NSArray 为json数据:

NSDictionary *dic;  
if ([NSJSONSerialization isValidJSONObject:dic]){  
    //isValidJSONObject 判断对象是否可以构建成json对象 
    NSError *error;  
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error];  
    //NSJSONWritingPrettyPrinted 把json数据格式化,否则会在一行中显示。更有可读性。  
    NSString *json =[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];  
}


2、JSONKit 

https://github.com/johnezang/JSONKit

#import "JSONKit.h"    (没有使用ARC   添加  -fno-objc-arc) 

解析json数据为 NSDictionary、NSArray :

NSData *data = dataes;//网络解析到dataes
NSDictionary *dic = [data  objectFromJSONData];

封装 NSDictionary 、 NSArray 为json数据:

NSDictionary *dic;
NSString *strJson = [dic JSONString];


5、SBJSON

http://stig.github.com/json-framework/

解析json数据为 NSDictionary、NSArray :

NSDictionary *dic;
SBJsonWriter *json = [[SBJsonWriter alloc] init];  
NSString *str = [json stringWithObject:dic];

封装 NSDictionary 、 NSArray 为json数据:

NSData *datas;
NSString * str = [[NSString alloc] initWithData: datas encoding:NSUTF8StringEncoding]; 
NSDictionary *dic = [[[SBJsonParser alloc] init] objectWithString:str];









转载于:https://my.oschina.net/liuchuanfeng/blog/659002

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值