IOS 解析Json数据(NSJSONSerialization)
● JSON是一种轻量级的数据格式,一般用于数据交互
● 服务器返回给客户端的数据,一般都是JSON格式或者XML格式(文件下载除 外)
● JSON的格式很像OC中的字典和数组 {"name" : "jack", "age" : 10}
{"names" : ["jack", "rose", "jim"]}
● 标准JSON格式的注意点:key必须用双引号
● 要想从JSON中挖掘出具体数据,得对JSON进行解析
● JSON 转换为 OC数据类型

● 第三方框架:JSONKit、SBJson、TouchJSON(性能从左到右,越差)
● 苹果原生(自带):NSJSONSerialization(性能最好)
● NSJSONSerialization的常见方法
● JSON数据 ! OC对象
+ (id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError **)error;
● OC对象 ! JSON数据
+ (NSData *)dataWithJSONObject:(id)obj options:(NSJSONWritingOptions)opt error:(NSError **)error;
三、JSON解析
1.利用NSJSONSerialization类解析
* JSON数据(NSData) --> Foundation-OC对象(NSDictionary、NSArray、NSString、NSNumber)
+ (id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError **)error; 2.JSON解析规律
* { } --> NSDictionary @{ }
* [ ] --> NSArray @[ ]
* " " --> NSString @" "
* 10 --> NSNumber @10
@interface HMViewController ()
@property (nonatomic, strong) NSArray *videos;
@end @implementation HMViewController - (void)viewDidLoad
{
[super viewDidLoad];
// 去除分割线
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; [MBProgressHUD showMessage:@"正在加载视频信息...."];
// NSXMLParser XML 解析
// MediaPlayer\AVFoundation // 访问服务器数据
NSString *urlStr = @"http://192.168.1.200:8080/MJServer/video"; // 发送请求
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; // GET
request.timeoutInterval = ; [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// 隐藏
[MBProgressHUD hideHUD]; if (data) {
// 解析json数据
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
NSArray *array = dict[@"videos"]; NSMutableArray *videos = [NSMutableArray array];
for (NSDictionary *videoDict in array) {
HMVideo *video = [HMVideo videoWithDict:videoDict];
[videos addObject:video];
}
self.videos = videos; // 刷新表格
[self.tableView reloadData];
} else {
[MBProgressHUD showError:@"网络繁忙!!!"];
}
}];
} #pragma mark - 数据源
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.videos.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ID = @"video";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
} HMVideo *video = self.videos[indexPath.row];
cell.textLabel.text = video.name;
cell.detailTextLabel.text = [NSString stringWithFormat:@"时长:%d分钟", video.length]; // video.image == resources/images/minion_01.png
NSString *imageUrl = [NSString stringWithFormat:@"http://192.168.1.200:8080/MJServer/%@", video.image];
[cell.imageView setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:@"placeholder"]]; return cell;
} - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return ;
} @end
IOS 解析Json数据(NSJSONSerialization)的更多相关文章
- iOS多线程与网络开发之解析json数据
郝萌主倾心贡献,尊重作者的劳动成果,请勿转载. // 同步发送信息 2 NSData *data = [NSURLConnection sendSynchronousRequest:request r ...
- ios 解析json,xml
一.发送用户名和密码给服务器(走HTTP协议) // 创建一个URL : 请求路径 NSString *urlStr = [NSString stringWithFormat:@"ht ...
- 使用Python解析JSON数据的基本方法
这篇文章主要介绍了使用Python解析JSON数据的基本方法,是Python入门学习中的基础知识,需要的朋友可以参考下: ----------------------------------- ...
- 使用jQuery解析JSON数据
我们先以解析上例中的comments对象的JSON数据为例,然后再小结jQuery中解析JSON数据的方法. 上例中得到的JSON数据如下,是一个嵌套JSON: {"comments&quo ...
- [转]javascript eval函数解析json数据时为什加上圆括号eval("("+data+")")
javascript eval函数解析json数据时为什么 加上圆括号?为什么要 eval这里要添加 “("("+data+")");//”呢? 原因在于: ...
- 用jquery解析JSON数据的方法以及字符串转换成json的3种方法
用jquery解析JSON数据的方法,作为jquery异步请求的传输对象,jquery请求后返回的结果是 json对象,这里考虑的都是服务器返回JSON形式的字符串的形式,对于利用JSONObject ...
- Android中使用Gson解析JSON数据的两种方法
Json是一种类似于XML的通用数据交换格式,具有比XML更高的传输效率;本文将介绍两种方法解析JSON数据,需要的朋友可以参考下 Json是一种类似于XML的通用数据交换格式,具有比XML更高的 ...
- fastjson生成和解析json数据,序列化和反序列化数据
本文讲解2点: 1. fastjson生成和解析json数据 (举例:4种常用类型:JavaBean,List<JavaBean>,List<String>,List<M ...
- 通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来的Json数据写入数据库表中
摘自:http://blog.csdn.net/mazhaojuan/article/details/8592015 通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来 ...
随机推荐
- python自动化day1
一.变量 变量定义的规则: 变量名只能是 字母.数字或下划线的任意组合 变量名的第一个字符不能是数字 以下关键字不能声明为变量名['and', 'as', 'assert', 'break', 'cl ...
- 修改阿里云ESC Centos 7.4 防火墙开放端口
例如系统:Centos 7.4操作如下 1,进入 cd /etc/firewalld/zones/ 目录 2,编辑 vim public.xml 3,按i或insert键进入编辑模式 4,在<z ...
- slf4j + loback配置
目前Java主流的log体系是 Slf4j +logback Spring boot 中配置log十分简单,常见的方式在application.yml文件中使用如下配置 logging: path: ...
- 缓存算法及Redis、Memcached、Guava、Ehcache中的算法
https://my.oschina.net/ffy/blog/501003 https://yq.aliyun.com/articles/622757 https://blog.csdn.net/s ...
- python3 unittest数据驱动
我们在自动化测试的时候,有没有遇到这样的问题?例如一个登录的接口要做自动化,会有很多case(用例),密码错误,密码正确这种.在继承unittest.TestCase的类中,凡是以“test”开头的方 ...
- Silverlight 鼠标双击 事件
Silverlight 双击事件例子 <UserControl x:Class="MouseDbClick.MainPage" xmlns="http://sche ...
- webview的进度条的加载,webview的使用以及handle的理解与使用
Webview的几个关键方法要介绍一些: 谷歌官方文档是这么说的; A WebView has several customization points where you can add your ...
- (转)轻松应对IDC机房带宽突然暴涨问题
原文:http://blog.51cto.com/oldboy/909696
- 二叉搜索树-php实现 插入删除查找等操作
二叉查找树(Binary Search Tree),(又:二叉搜索树,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值: 若它的 ...
- MVC controller序列化下拉框给view
在开发中遇到的小问题,一个下拉框,一个文本域 ,文本域根据下拉框变化: 由于是一次全部取出的值,下拉框变化不想再去取值: 在后台把值先序列化给前台用 controller: List<Lesso ...