● 什么是JSON

● JSON是一种轻量级的数据格式,一般用于数据交互

● 服务器返回给客户端的数据,一般都是JSON格式或者XML格式(文件下载除 外)

● JSON的格式很像OC中的字典和数组 {"name" : "jack", "age" : 10}

{"names" : ["jack", "rose", "jim"]}

●  标准JSON格式的注意点:key必须用双引号 
  • ●  要想从JSON中挖掘出具体数据,得对JSON进行解析

  • ●  JSON 转换为 OC数据类型

●  在iOS中,JSON的常见解析方案有4种

  • ●  第三方框架: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)的更多相关文章

  1. iOS多线程与网络开发之解析json数据

    郝萌主倾心贡献,尊重作者的劳动成果,请勿转载. // 同步发送信息 2 NSData *data = [NSURLConnection sendSynchronousRequest:request r ...

  2. ios 解析json,xml

    一.发送用户名和密码给服务器(走HTTP协议) // 创建一个URL : 请求路径    NSString *urlStr = [NSString stringWithFormat:@"ht ...

  3. 使用Python解析JSON数据的基本方法

    这篇文章主要介绍了使用Python解析JSON数据的基本方法,是Python入门学习中的基础知识,需要的朋友可以参考下:     ----------------------------------- ...

  4. 使用jQuery解析JSON数据

    我们先以解析上例中的comments对象的JSON数据为例,然后再小结jQuery中解析JSON数据的方法. 上例中得到的JSON数据如下,是一个嵌套JSON: {"comments&quo ...

  5. [转]javascript eval函数解析json数据时为什加上圆括号eval("("+data+")")

    javascript eval函数解析json数据时为什么 加上圆括号?为什么要 eval这里要添加 “("("+data+")");//”呢?   原因在于: ...

  6. 用jquery解析JSON数据的方法以及字符串转换成json的3种方法

    用jquery解析JSON数据的方法,作为jquery异步请求的传输对象,jquery请求后返回的结果是 json对象,这里考虑的都是服务器返回JSON形式的字符串的形式,对于利用JSONObject ...

  7. Android中使用Gson解析JSON数据的两种方法

    Json是一种类似于XML的通用数据交换格式,具有比XML更高的传输效率;本文将介绍两种方法解析JSON数据,需要的朋友可以参考下   Json是一种类似于XML的通用数据交换格式,具有比XML更高的 ...

  8. fastjson生成和解析json数据,序列化和反序列化数据

    本文讲解2点: 1. fastjson生成和解析json数据 (举例:4种常用类型:JavaBean,List<JavaBean>,List<String>,List<M ...

  9. 通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来的Json数据写入数据库表中

    摘自:http://blog.csdn.net/mazhaojuan/article/details/8592015 通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来 ...

随机推荐

  1. 求用1g、2g、3g的砝码(每种砝码有无穷多个)称出10g的方案有几种

    #include <iostream> using namespace std; // ; // sup是保存多项式的数组,sup[n]中的值代表指数为i的系数 ,下标i是x的指数 // ...

  2. eclipse.ini X64 Oxygen.2 Release (4.7.2) lombok

    X64 Eclipse Java EE IDE for Web Developers. Version: Oxygen.2 Release (4.7.2)Build id: 20171218-0600 ...

  3. vue快速入门(二)

    工程搭建完成,接下来如何使用. 首先找到src\rooter\index.js文件 这里是路由文件配置要访问的组件,这个会在后期说明 这里的components/A 是组件里边的 需要手动 创建A.v ...

  4. OS---存储器

    1.存储器的层次结构 1.1 概述 理想情况下,存储器应当速度非常快.并且与处理器的速度匹配.容量大且价格低廉: 实际情况,无法满足上述三个条件: 于是在现在OS中,存储器采用  层次结构  来组织: ...

  5. spring事务的传播性

    <!--配置事务传播特性 --><tx:advice id = "txAdvice" transaction-manager = "txManage&q ...

  6. python3 FTP简单实现文件下载(含中文乱码问题)

    from ftplib import FTP def ftp_down(HOST,romatepath,filename,localpath): user=***** password=***** f ...

  7. informix(南大通用)sql语法的差异

    1.create view  444(...)  as select ...from... 2.insert into select.......union  select     不支持 请分开写 ...

  8. IntelliJ IDEA里找不到javax.servlet的jar包

    此处有小坑,请注意: https://mvnrepository.com网站查询到的servlet的包的格式为: provided group: 'javax.servlet', name: 'jav ...

  9. 导出csv文件时韩文乱码解决方法

    从asp.net导出csv这样配置可以防止韩文等乱码,在头部加上0xEF, 0xBB, 0xBF: string fileName = "attachment;filename=" ...

  10. C#取得程序的根目录以及判断文件是否存在

    一:获取根目录的方法 取得控制台应用程序的根目录方法方法1.Environment.CurrentDirectory 取得或设置当前工作目录的完整限定路径方法2.AppDomain.CurrentDo ...