JSON解析
1. 利用JSONKit第三方库解析json文件
1) 下载JSONKit https://github.com/johnezang/JSONKit
2) 将JSONKit.h和JSONKit.m copy到我们应用的工程中
3) 注意是不是ARC
- (void)parseUseJSONKit { NSURL *url = [NSURL URLWithString:@"http://m.weather.com.cn/data/101180701.html"]; NSString *jsonString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil]; NSDictionary *weatherDic = [jsonString objectFromJSONString]; NSDictionary *weatherInfo = [weatherDic objectForKey:@"weatherinfo"]; m_txtView.text = [NSString stringWithFormat:@"今天是 %@ %@ %@ 的天气状况是: %@ %@\n%@:%@",[weatherInfo objectForKey :@"date_y"],[weatherInfo objectForKey :@"week"],[weatherInfo objectForKey :@"city"],[weatherInfo objectForKey :@"weather1"],[weatherInfo objectForKey :@"temp1"],[weatherInfo objectForKey :@"index"],[weatherInfo objectForKey :@"index48_d"]]; }
2. 利用SBJson第三方库解析json文件
1) 下载SBJson https://github.com/stig/json-framework
2) 编译下载号的工程,将静态库libsbjson-ios.a文件导入到我们应用的工程中,并且将相应的头文件导入到应用中(其中包括: SBJson.h,SBJsonParser.h, SBJsonWriter.h, SBJsonStreamParser.h, SBJsonStreamParserAdapter.h, SBJsonStreamWriter.h, SBJsonStreamTokeniser.h, SBJsonStreamWriter.h, SBJsonStreamParserAdapter.h)
3) 进入” SBJson.h”,这个文件是支持ARC的
4) 使用过程如下:
- (void)parseUseSBJson { NSURL *url = [NSURL URLWithString:@"http://m.weather.com.cn/data/101180701.html"]; NSString *jsonString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil]; SBJsonParser *parser = [[SBJsonParser alloc]init]; NSDictionary *rootDic = [parser objectWithString:jsonString]; NSDictionary *weatherInfo = [rootDic objectForKey:@"weatherinfo"]; m_txtView.text = [NSString stringWithFormat:@"今天是 %@ %@ %@ 的天气状况是: %@ %@\n%@:%@",[weatherInfo objectForKey :@"date_y"],[weatherInfo objectForKey :@"week"],[weatherInfo objectForKey :@"city"],[weatherInfo objectForKey :@"weather1"],[weatherInfo objectForKey :@"temp1"],[weatherInfo objectForKey :@"index"],[weatherInfo objectForKey :@"index48_d"]]; }
3. iOS SDK自动带的NSJSONSerialization类解析json文件
直接使用NSJSONSerialization类,如下:
- (void)parseUseNSJSONSerialization { NSURL *url = [NSURL URLWithString:@"http://m.weather.com.cn/data/101180701.html"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:nil]; NSDictionary *weatherInfo = [weatherDic objectForKey:@"weatherinfo"]; m_txtView.text = [NSString stringWithFormat:@"今天是 %@ %@ %@ 的天气状况是: %@ %@\n%@:%@",[weatherInfo objectForKey :@"date_y"],[weatherInfo objectForKey :@"week"],[weatherInfo objectForKey :@"city"],[weatherInfo objectForKey :@"weather1"],[weatherInfo objectForKey :@"temp1"],[weatherInfo objectForKey :@"index"],[weatherInfo objectForKey :@"index48_d"]]; }
4. 利用TouchJson第三方库解析json文件
1) 下载TouchJson包https://github.com/TouchCode/TouchJSON
2) 解压后将source文件夹中的类文件导入到我们应用的工程中
3) 注意:类库用额是非ARC
4) 引入CJSONDeserializer.h
5) 应用如下:
- (void)parseUseTouchJson { NSURL *url = [NSURL URLWithString:@"http://m.weather.com.cn/data/101180701.html"]; NSString *jsonString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil]; NSDictionary *rootDic = [[CJSONDeserializer deserializer]deserialize:[jsonString dataUsingEncoding:NSUTF8StringEncoding] error:nil]; NSDictionary *weatherInfo = [rootDic objectForKey:@"weatherinfo"]; m_txtView.text = [NSString stringWithFormat:@"今天是 %@ %@ %@ 的天气状况是: %@ %@\n%@:%@",[weatherInfo objectForKey :@"date_y"],[weatherInfo objectForKey :@"week"],[weatherInfo objectForKey :@"city"],[weatherInfo objectForKey :@"weather1"],[weatherInfo objectForKey :@"temp1"],[weatherInfo objectForKey :@"index"],[weatherInfo objectForKey :@"index48_d"]]; }
5. 利用NextiveJson第三方库解析json文件
1) 下载NextiveJson第三方库 https://github.com/nextive/NextiveJson
2) 应用工程设置为非ARC
3) 将Extensions.h, Extensions.m, NXDebug.h, NXDebug.m, NXJsonParser.h, NXJsonParser.m, NXJsonSerializer.h, NXJsonSerializer.m, NXSerializable.h 文件copy到工程中
4) 使用NextiveJson,解析json文件,如下:
- (void)parseUseNextiveJson { NSURL *url = [NSURL URLWithString:@"http://m.weather.com.cn/data/101180701.html"]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; NSDictionary *weatherDic = [NXJsonParser parseData:responseData error:nil ignoreNulls:NO]; NSDictionary *weatherInfo = [weatherDic objectForKey:@"weatherinfo"]; m_txtView.text = [NSString stringWithFormat:@"今天是 %@ %@ %@ 的天气状况是: %@ %@\n%@:%@",[weatherInfo objectForKey :@"date_y"],[weatherInfo objectForKey :@"week"],[weatherInfo objectForKey :@"city"],[weatherInfo objectForKey :@"weather1"],[weatherInfo objectForKey :@"temp1"],[weatherInfo objectForKey :@"index"],[weatherInfo objectForKey :@"index48_d"]]; }
相关推荐
本着探究 JSON 原理的目的,我将会在这DEMO中实现了一个简单的JSON解析器。由于 JSON 本身比较简单,解析起来也并不复杂。所以如果大家感兴趣的话,在看完本DEMO后,不妨自己动手实现一个 JSON 解析器。
易语言、易Json解析模块、水淼Json 易语言水淼JSON解析模块源码自主解析JSON数据类型,使用树来直观清晰显示出所有结构,辅助程序员快速定位JSON节点提高开发效率。
C#对多级json解析,非常实用,此代码写在load事件里面,然后可以了解解析json的原理。尤其是多级解析。
自己写的json解析,不使用第三方的jar包,简单易懂,分为本地json解析和online json解析,网上的只需要修改一下url地址即可
JSON 解析 http://blog.csdn.net/zxw136511485/article/details/51451523
易语言JSON解析模块2.0源码例程程序结合易语言扩展界面支持库和应用接口支持库,调用API函数实现JSON解析、生成、编辑。点评:易语言JSON解析模块2.0源码通过封装JSON类形成稳定强大的json处理核心。资源作者:。...
sql server 2014 JSON解析到表函数 CREATE FUNCTION [dbo].[parseJSON]( @JSON NVARCHAR(MAX)) RETURNS @hierarchy TABLE ( element_id INT IDENTITY(1, 1) NOT NULL, ...
易语言页面订单json解析源码,页面订单json解析,初始化表格,从网页源码中读取订单到数组,将订单数组内容写到表格
Unity能够在WebGL包使用的json解析.dll
c# json 解析+C# newtonsoft.json.dll 解析类库文件。
Json解析,Gson解析,本地,网络解析简单的案例
最简单最高效的Json解析,快捷小巧方便简洁
delphi_json解析格式化源码 delphi_json解析格式化源码 delphi_json解析格式化源码
PB Json解析库
JSON解析最新版20151017超简JSON效验解析器
json解析的三种方式
C#中Json 解析类库,C# 解析json 时,通常使用的是强类型对象反序列化,但是有时候不知道具体的对象时,使用该类库,使用dictionary 解析弱类型数据
最全的 json解析JAR包 最全的 json解析JAR包 最全的 json解析JAR包
适用于json解析,美观直观的观看数据
网上有CJson来解析数据,但对于单片机来说还是比较繁杂。就自己写了个JSON解析程序,非常精简,只有一百多行。 当然肯定也有不少BUG,欢迎大家指证、优化。