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封装中的使用的更多相关文章

  1. AJAX如何接收JSON数据

    简介 在我们了解如何使用AJAX返回JSON数据的时候要先明白下列几点 1. JSON如何来表示对象的 2. JSON如何来表示数组的 var object = { "labId" ...

  2. ThinkPHP中使用ajax接收json数据的方法

    本文实例讲述了ThinkPHP中使用ajax接收json数据的方法.分享给大家供大家参考.具体分析如下: 这里通过ThinkPHP+jquery实现ajax,扩展了下,写了个查询,前台代码如下: 首先 ...

  3. 后端接收json数据交互

    学习记录,后端接收json数据几种方式 1.直接接收或者通过HttpServletRequest接收 public void test(String userid, HttpServletReques ...

  4. Beego框架POST请求接收JSON数据

    原文: https://blog.csdn.net/Aaron_80726/article/details/83870563 ------------------------------------- ...

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

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

  6. Ajax前台返回JSON数据后再Controller中直接转换成类型使用,后台接收json转成实体的方法

    之前写过一篇记录文章,写的是将一个比较复杂的数据结构在前台组合起来后传递到后台. 当时并不太了解@RequestBody,也并没有使用js提供的JSON.stringify()方法 所有都是自己写的, ...

  7. .NET发送请求(get/post/http/https),携带json数据,接收json数据

    C#发送https请求有一点要注意: ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateVa ...

  8. java后台接收json数据,报错com.alibaba.fastjson.JSONObject cannot be cast to xxx

    从前台接收json封装的list数据,在后台接收时一直报错,com.alibaba.fastjson.JSONObject cannot be cast to xxx, 使用这种方式接收可以接收 @R ...

  9. ajax接收json数据到js解析

    今天又学到了一点新知识,脑子记不住东西特把它记录下来! 页面ajax请求后台时一般都是返回字符串进行判断,要是返回list或者对象时该怎么办? 第一种:ajax接收到list并返回给前台 js代码: ...

随机推荐

  1. PHP实现选择排序

    选择排序: 选择排序(Selection sort)是一种简单直观的排序算法.它的工作原理如下.首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余未排序元素中继续寻找最小( ...

  2. 17-7-26-react-router入门

    完蛋了,上司最近都和颜悦色的,搞得我跑路的决心大大减退,不过还是要跑路.毕竟面向工资编程.今天一天基本都在挑项目的CSS,由于是别人哪里直接copy的,然后css我也直接copy的codepen上的源 ...

  3. POJ 1860 Currency Exchange【SPFA判环】

    Several currency exchange points are working in our city. Let us suppose that each point specializes ...

  4. NOI2015 D1T2 软件包管理器

    题目传送门; 这个貌似是我这个蒟蒻做的第一道NOI系列的题了吧...这题的算法是树链剖分,其实基本上就是很常见的树剖+线段树,题目既然是要求每次安装或卸载改变的软件包的数目,那么就在每次操作前记录下线 ...

  5. python笔记四:面向对象

    1.类 class Student(object): def __init__(self, name, score): self.name = name self.score = score 1)__ ...

  6. POJ 3662 Telephone Lines (分层图)

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6785   Accepted: 2498 D ...

  7. ubuntu14.04下安装爬虫工具scrapy

    scrapy是目前准备要学习的爬虫框架,其在ubuntu14.04下的安装过程如下: ubuntu14.04下默认安装了2.7的python以及setuptools,若未安装,可通过下面指令安装: s ...

  8. EF 通用数据层类

    EF 通用数据层父类方法小结 转载:http://www.cnblogs.com/yq-Hua/p/4165344.html MSSql 数据库 数据层 父类 增删改查: using System; ...

  9. [HTML/CSS]说说position

    目录 语法 作用 使用定位的条件 总结 语法 position : static absolute relative(static.absolute.relative常用值). 参数 static : ...

  10. Vue2.X的路由管理记录之 钩子函数(切割流水线)2

    $route可以在子组件任何地方调用,代表当前路由对象,这个属性是只读的,里面的属性是 immutable(不可变) 的,不过你可以 watch(监测变化) 它. 导航和钩子函数: 导航:路由正在发生 ...