iOS key value coding kvc在接收json数据与 model封装中的使用
iOS key value coding kvc在接收json数据与 model封装中的使用
使用 kvc 能够极大的简化代码工作,及以后的接口维护工作;
1:先创建MovieModel类.h和 .m
注意Model类的属性根据 后台接口返回的 json数据 里面的字段对应,一一对应;
// Created by cocoajin on 14-1-15.
// Copyright (c) 2014年 www.zhgu.net. All rights reserved.
// #import <Foundation/Foundation.h> @interface MovieModel : NSObject @property (nonatomic,strong)NSString *alt;
@property (nonatomic,strong)NSNumber *collect_count;
@property (nonatomic,strong)NSString *original_title;
@property (nonatomic,strong)NSString *title;
@property (nonatomic,strong)NSNumber *year;
@property (nonatomic,strong)NSString *subtype;
@property (nonatomic,strong)NSNumber *id;
@property (nonatomic,strong)NSDictionary *images;
@property (nonatomic,strong)NSDictionary *rating; - (id)initWith:(NSDictionary *)aDic; @end
#import "MovieModel.h" @implementation MovieModel - (id)initWith:(NSDictionary *)aDic
{
self = [super init]; if (self) {
[self setValuesForKeysWithDictionary:aDic];
} return self;
} - (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
NSLog(@"未定义的key:%@",key);
} - (NSString *)description
{
return [NSString stringWithFormat:@"thie movie is :alt=%@ ,collect_count=%@ ,original_title=%@ ,title=%@ ,year=%@ ,subtype=%@, id=%@ ,image=%@ ,rating=%@",self.alt,self.collect_count,self.original_title,self.title,self.year,self.subtype,self.id,self.images,self.rating];
}
2:接口请求处理部分:
NSString *douBanApi = @"http://api.douban.com/v2/movie/top250?count=5";
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:douBanApi]]; __block ASIHTTPRequest *brRequest = request; [request setCompletionBlock:^{
id json = [NSJSONSerialization JSONObjectWithData:[brRequest responseData] options: error:Nil];
//NSLog(@"%@",json);
NSLog(@"completed"); NSDictionary *dataDic = [NSDictionary dictionaryWithDictionary:[[json objectForKey:@"subjects"] objectAtIndex:]];
NSLog(@"dataDic = %@",dataDic); MovieModel *movie = [[MovieModel alloc]initWith:dataDic];
NSLog(@"%@",movie); }]; [request setFailedBlock:^{
NSLog(@"%@",[brRequest error]);
}]; [request startAsynchronous];
3:请求处理的log结果
iOS key value coding kvc在接收json数据与 model封装中的使用的更多相关文章
- AJAX如何接收JSON数据
简介 在我们了解如何使用AJAX返回JSON数据的时候要先明白下列几点 1. JSON如何来表示对象的 2. JSON如何来表示数组的 var object = { "labId" ...
- ThinkPHP中使用ajax接收json数据的方法
本文实例讲述了ThinkPHP中使用ajax接收json数据的方法.分享给大家供大家参考.具体分析如下: 这里通过ThinkPHP+jquery实现ajax,扩展了下,写了个查询,前台代码如下: 首先 ...
- 后端接收json数据交互
学习记录,后端接收json数据几种方式 1.直接接收或者通过HttpServletRequest接收 public void test(String userid, HttpServletReques ...
- Beego框架POST请求接收JSON数据
原文: https://blog.csdn.net/Aaron_80726/article/details/83870563 ------------------------------------- ...
- 通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来的Json数据写入数据库表中
摘自:http://blog.csdn.net/mazhaojuan/article/details/8592015 通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来 ...
- Ajax前台返回JSON数据后再Controller中直接转换成类型使用,后台接收json转成实体的方法
之前写过一篇记录文章,写的是将一个比较复杂的数据结构在前台组合起来后传递到后台. 当时并不太了解@RequestBody,也并没有使用js提供的JSON.stringify()方法 所有都是自己写的, ...
- .NET发送请求(get/post/http/https),携带json数据,接收json数据
C#发送https请求有一点要注意: ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateVa ...
- java后台接收json数据,报错com.alibaba.fastjson.JSONObject cannot be cast to xxx
从前台接收json封装的list数据,在后台接收时一直报错,com.alibaba.fastjson.JSONObject cannot be cast to xxx, 使用这种方式接收可以接收 @R ...
- ajax接收json数据到js解析
今天又学到了一点新知识,脑子记不住东西特把它记录下来! 页面ajax请求后台时一般都是返回字符串进行判断,要是返回list或者对象时该怎么办? 第一种:ajax接收到list并返回给前台 js代码: ...
随机推荐
- Python与SQLite日期时间函数的使法
SQLite的时间函数跟Python的时间函数有些许差别,所以稍做记录,供自己以后查询. 网上有将SQLite官方WIKI内容翻译成中文的文章,大家有兴趣可以搜索一下,我这里单纯记录一下个人比较常用的 ...
- STM32通用定时器实现LED灯闪烁
刚才看了一下STM32通用定时器的教程,其实和51的定时器使用差不多.只是因为32的时钟更复杂,可操控的寄存器更多,所以写的时候可能更复杂. 使用通用定时器中断的一般步骤:1.使能定时器时钟 这个需要 ...
- zookeeper,hadoop安装部署其实与防火墙无关
网上查看了很多人关于hadoop,zookeeper的文章,大多都把关闭防火墙作为首要前提,个人觉得这大可不必. 首先你需要知道你部署的是什么东西,它需要哪些端口即可.把相关端口打开就可以了啊.然后把 ...
- SCU - 4441 Necklace(树状数组求最长上升子数列)
Necklace frog has \(n\) gems arranged in a cycle, whose beautifulness are \(a_1, a_2, \dots, a_n\). ...
- HDU 6041 I Curse Myself(二分+搜索)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6041 [题目大意] 给出一个仙人掌图,求第k小生成树 [题解] 首先找到仙人掌图上的环,现在的问题 ...
- BZOJ 3790 神奇项链(manacher+DP+树状数组)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3790 [题目大意] 问最少用几个回文串可以构成给出串,重叠部分可以合并 [题解] 我们 ...
- Eclipse运行Maven的SpringMVC项目Run on Server时出现错误:Error configuring application listener of class org.springframework.web.context.ContextLoaderListener的问题解决
错误: 严重: Error configuring application listener of class org.springframework.web.context.ContextLoade ...
- 打印出C# 中float ,double 在内存中的存放形式
float floatA = 2.2f; ); ; i < ;++i ) { uint temp = 0x80000000 & (a << i); ) { Console.W ...
- vue-router 2.0 改变的内容
2.x 版本的 vue-router 相比之前的0.7.x版本,有很多破坏性改变: 通用 API 的修改 The old router.go() is now router.push() . 新的 r ...
- java多线程处理导入数据拆分List集合 同步处理插入数据
原文:https://www.2cto.com/kf/201612/581174.html import org.apache.log4j.Logger; import org.apache.poi. ...