引入#import "JSONKit.h"
下面主要差别在于中间转化的死NSString还是NSData类型
1、
NSURL *url =[NSURL URLWithString:@"http://www.weather.com.cn/data/cityinfo/101010100.html"];
NSString *jsonStr =[NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSData *jsondata = [jsonStr dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dic =[jsondata objectFromJSONData];
NSDictionary *dic1 =[dic objectForKey:@"weatherinfo"];
NSLog(@"%@", [dic1 objectForKey:@"city"]);
2、创建JSONDecoder类进行解析
NSURL *url =[NSURL URLWithString:@"http://www.weather.com.cn/data/cityinfo/101010100.html"];
NSData *jsonData = [NSData dataWithContentsOfURL:url];
JSONDecoder *decoder =[JSONDecoder new];
id jsondata = [decoder objectWithData:jsonData];
NSDictionary *dic =(NSDictionary*)jsondata;
NSDictionary *dic1 =[dic objectForKey:@"weatherinfo"];
NSLog(@"%@", [dic1 objectForKey:@"city"]);
3、
NSURL *url =[NSURL URLWithString:@"http://www.weather.com.cn/data/cityinfo/101010100.html"];
NSString *jsonStr =[NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSDictionary *dic = [jsonStr objectFromJSONStringWithParseOptions:JKParseOptionLooseUnicode];
NSDictionary *dic1 =[dic objectForKey:@"weatherinfo"];
NSLog(@"%@", [dic1 objectForKey:@"city"]);
我们现在使用的JSONKit有许多问题我们需要进行修改之后才可以使用
1、TARGETS->Build Phases->Compile Sources中找到JSONKit.m文件在后面双击添加 -fno-objc-arc
2、JSONKit中运用了很多isa,因此有两种修改方式,改成其中一种即可,建议第二种
(1)Build Setting中搜索框中输入isa进行搜索,下面会显示Direct usage of 'isa' 将后边改成NO
(2)在JSONKit.m文件中进行修改: