//
// ViewController.m
// Json
//
// Created by City--Online on 15/4/28.
// Copyright (c) 2015年 CYW. All rights reserved.
//
#define strUrl @"http://app.api.autohome.com.cn/autov3.2/news/newslist-a2-pm1-v3.2.0-c0-nt0-p1-s20-l0.html"
#import "ViewController.h"
#import "SBJson.h"
#import "CJSONDeserializer.h"
#import "CJSONSerializer.h"
#import "JSONKit.h"
@interface ViewController ()
@property(nonatomic,strong)NSString *stringData;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// json解析
NSURL *url=[NSURL URLWithString:strUrl];
NSError *err=NULL;
//获取Json字符串
NSString *Jsonstr=[NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&err];
NSData *data=[Jsonstr dataUsingEncoding:NSUTF8StringEncoding];
// 第一种:IOS5以上自带的JSONObjectWithData
// typedef NS_OPTIONS(NSUInteger, NSJSONReadingOptions) {
// NSJSONReadingMutableContainers = (1UL << 0),//返回可变容器,NSMutableDictionary或NSMutableArray
// NSJSONReadingMutableLeaves = (1UL << 1),//返回的JSON对象中字符串的值为NSMutableString
// NSJSONReadingAllowFragments = (1UL << 2)//允许JSON字符串最外层既不是NSArray也不是NSDictionary,但必须是有效的JSON Fragment。例如使用这个选项可以解析 @“123” 这样的字符串。
// }
//解析
NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&err];
if (err==nil) {
NSLog(@"%@",[dic objectForKey:@"message"]);
NSArray *array=[[dic objectForKey:@"result"] objectForKey:@"focusimg"];
NSLog(@"%@",[array objectAtIndex:1]);
}
//生成
//判断是否能转为Json
if ([NSJSONSerialization isValidJSONObject:dic]) {
//转为Json
NSData *data= [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
NSString *str=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",str);
//json再次转为字典
NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&err];
NSLog(@"%@",dic);
}
// 第二种:SBJson
// 解析
//引入#import "SBJson.h"
SBJsonParser *parser=[[SBJsonParser alloc]init];
NSDictionary *SBJsonDic=[parser objectWithString:Jsonstr error:nil];
NSLog(@"%@",SBJsonDic);
SBJsonWriter *sbwriter=[[SBJsonWriter alloc]init];
NSString *sbstr=[sbwriter stringWithObject:SBJsonDic];
NSLog(@"%@",sbstr);
// 第三种:Touch Json
// 解析
// #import "TouchJson/JSON/CJSONDeserializer.h"
NSDictionary *touchDic=[[CJSONDeserializer deserializer] deserialize:data error:nil];
NSLog(@"%@",touchDic);
// 生成
NSString *touchstr=[[NSString alloc]initWithData: [[CJSONSerializer serializer] serializeDictionary:touchDic error:nil] encoding:NSUTF8StringEncoding];
NSLog(@"%@",touchstr);
// 第四种:JsonKit
// 使用Jsonkit时引入第三方稍微麻烦
// 第一步:引入Jsonkit
// 第二步:JsonKit支持MRC,不支持ARC(伟哥指导纠正thanks)。点击Targets->Build Phases->Compile Sources中找到JsonKit.m点击输入-fno-objc-arc
// 第三步:在Build Setting中Levels中搜索 Direct usage of 'isa'设置为NO
//生成
NSDictionary *JsonKitDic=[Jsonstr objectFromJSONString];
NSLog(@"%@",JsonKitDic);
//解析
NSString *JsonKitstr=[JsonKitDic JSONString];
NSLog(@"%@",JsonKitstr);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Json比较简单,直接上代码,具体说明找度娘,JsonKit在ios9中会出现闪退报错 需要下载最新的JsonKit