iOS原生JSON解析.
- (IBAction)accessInterfaceBtnPressed:(id)sender {
NSError *error;
NSString *URL=@"https://api.thinkpage.cn/v3/life/suggestion.json?key=aacuhmmvipa30v3f&location=shanghai&language=zh-Hans";
//加载一个NSURL对象
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:URL]];
//将请求的url数据放到NSData对象中
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中
NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
// NSLog(@"All: %@", weatherDic );
NSDictionary *weatherInfo = [weatherDic objectForKey:@"results"];
NSArray * resultArray=[weatherDic objectForKey:@"results"];
// NSLog(@"dictionary length is: %d",[weatherInfo count]);
// NSLog(@"weatherInfo: %@", weatherInfo );
// NSString *updateTime=[weatherInfo objectForKey:@"last_update"];
NSLog(@"updateTime: %@", [weatherInfo valueForKey:@"last_update"] );
//[resultArray valueForKey:@"location"]valueForKey:@"name" ]
NSArray *name=[[resultArray valueForKey:@"location"]valueForKey:@"name"];
NSString *shanghai=name[0];
//NSString *stringData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// NSString *resultStr=[[NSString alloc]initWithUTF8String:@"aaa"];
NSLog(@"shanghai: %@", shanghai);
NSDictionary *locationInfo = [weatherInfo valueForKey:@"location"];
//NSLog(@"last_update: %@", [weatherInfo objectForKey:@"last_update"] );
// NSMutableString *name=[locationInfo valueForKey:@"name"];
// NSLog(@"name: %@", name);
// NSString*jsonString = [[NSString alloc]initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding] ;
// NSData *data=[locationInfo valueForKey:@"name"];
[self textView].text = shanghai;
}
参考:
https://temboo.com/ios/parsing-json
| How it appears in the JSON string | |
|---|---|
| NSArray | "name": [ |
| NSObject | "name": { |
另外, 原来()是特么一个数组的意思...搞得我各种找编码的方法...嫩娘...怪我罗....
iOS原生JSON解析.的更多相关文章
- 原生JSON解析
原生JSON解析 JSONObject:JSON数据封装对象JSONArray:JSON数据封装数组 布局: <?xml version="1.0" encoding=&qu ...
- iOS中JSON解析三方库的比较
网络数据解析框架 1. JsonModel 一个 JSON 模型转换库,有着比较简洁的接口.Model 需要继承自 JSONModel. 2. yyModel yyModel比较轻量(算上.h 只 ...
- IOS中Json解析的四种方法
作为一种轻量级的数据交换格式,json正在逐步取代xml,成为网络数据的通用格式. 有的json代码格式比较混乱,可以使用此“http://www.bejson.com/”网站来进行JSON格式化校验 ...
- 【转】IOS中Json解析的四种方法
原文网址:http://blog.csdn.net/enuola/article/details/7903632 作为一种轻量级的数据交换格式,json正在逐步取代xml,成为网络数据的通用格式. 有 ...
- iOS开源JSON解析库MJExtension
iOS中JSON与NSObject互转有两种方式:1.iOS自带类NSJSONSerialization 2.第三方开源库SBJSON.JSONKit.MJExtension.项目中一直用MJExte ...
- iOS开发-JSON解析
JSON(JavaScript Object Notation)在网络传输中几乎无处不在,JSON是一种轻量级的数据交换格式,是基于JavaScript(Standard ECMA-262 3rd E ...
- iOS 中json解析数据出现中文乱码的问题
一般服务器的编码格式都是UTF8,这样通过json解析下来的的数据,一般中文是不会出现乱码,但是如果服务器的编码格式不是UTF8,通过json解析的数据中的中文容易出现luan乱码,怎么解决这个问题呢 ...
- .Net Core 3.0原生Json解析器
微软官方博客中描述了为什么构造了全新的Json解析器而不是继续使用行业准则Json.Net 微软博客地址:https://devblogs.microsoft.com/dotnet/try-the-n ...
- iOS SDK原生JSON解析
- (IBAction)touchReadButton:(id)sender { NSData *jsonData = [[NSData alloc] initWithContentsOfFile:J ...
随机推荐
- iOS ASIHTTPRequest 使用指南
http://www.devdiv.com/iOS_iPhone-ASIHTTPRequest使用指南---_lt__lt_翻译稿_gt__gt_---连载-thread-93741-1-1.html
- Java实现热替换
package test; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.nio. ...
- PaySignKey
关键词:微信支付 PaySignKey 原文:http://www.cnblogs.com/txw1958/p/weixin-paysignkey.html 微信支付现在分为v2版和v3版 2014年 ...
- (copy) Linux Commands Cheat Sheet in Black & White
source: http://linoxide.com/linux-command/linux-commands-cheat-sheet/
- Function.prototype.bind接口浅析
本文大部分内容翻译自 MDN内容, 翻译内容经过自己的理解. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Glo ...
- Python:使用psycopg2模块操作PostgreSQL
安装psycopg2模块: 怎么验证是否已经安装过psycopy2? 编写上面代码,运行看是否抛出缺少psycopg2模块. 安装方法1: 1)使用psycopg2-2.4.2.win-amd64-p ...
- Leetcode: Palindrome Numbers
Determine whether an integer is a palindrome. Do this without extra space. 尝试用两头分别比较的方法,结果发现无法解决1000 ...
- html 字体加粗
<font style="font-weight: bold;">无敌小昆虫</font> <font>无敌小昆虫</font> f ...
- Codeforce Round #216 Div2
e,还是写一下这次的codeforce吧...庆祝这个月的开始,看自己有能,b到什么样! cf的第二题,脑抽的交了错两次后过了pretest然后system的挂了..脑子里还有自己要挂的感觉,果然回头 ...
- 树形DP +01背包(HDU 1011)
题意:有n个房间,有n-1条道路连接着n个房间,每个房间都有若干个野怪和一定的能量值,有m个士兵从1房间入口进去,到达每个房间必须要留下若干士兵杀死所有的野怪,然后其他人继续走,(一个士兵可以杀死20 ...