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数据,将前台传来 ...
随机推荐
- http文件上传/下载
package unit; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputSt ...
- asp.net core 标签验证函数功能
public class AuthFilter : Attribute, IActionFilter { public void OnActionExecuted(ActionExecutedCont ...
- NMS_非极大值抑制(转)
NMS(non maximum suppression),中文名非极大值抑制,在很多计算机视觉任务中都有广泛应用,如:边缘检测.目标检测等. 这里主要以人脸检测中的应用为例,来说明NMS,并给出Mat ...
- Angular JS 1.X 接口拿不到 http post 请求的数据
app上加上配置相关的代码即可 var myApp = angular.module('myApp',[]); myApp.config(function($httpProvider){ $httpP ...
- Unity Animation.CrossFade Animation.Play
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerCo ...
- [转] js 实现table每列可左右拖动改变列宽度
<!DOCTYPE HTML> <html> <head> <meta charset="gbk"> <tit ...
- C#:新邮件监听及搜索
在项目中,我们需要监听邮件服务器,看是否有新的邮件进入.下面的代码可以帮助我们监听新邮件,并对已有的邮件进行查找: using System; using System.Collections.Gen ...
- linux下常用命令备忘
转自:Linux 命令集锦 linux下查看监听端口对应的进程 # lsof -i:9000 # lsof -Pnl +M -i4 如果退格键变成了:"^h". 终端连接unix删 ...
- Devexpress Xtrareports 创建多栏报表
根据官方回答:多列或多行(取决于当前的多栏设置)呈现数据的报表 这种报表是有用的,例如,当每个明细区都只显示少量数据.并且需要在一列的右侧打印下一个明细区时,这样就能充分利用整个页面的宽度,此外,当创 ...
- 查看SQL Server中的锁表及解锁
有时候系统很慢,有可能是SQL Server数据库中某些表被锁定 --查看被锁表(需查多几次,有些临时锁很快会自动解锁): SELECT request_session_id AS spid, OBJ ...