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. CSS3主要的几个样式笔记

    1.边框:border-color:    设置对象边框的颜色.     使用CSS3的border-radius属性,如果你设置了border的宽度是X px,那么你就可以在这个border上使用X ...

  2. 洛谷——P2035 iCow

    P2035 iCow 题目描述 被无止境的农活压榨得筋疲力尽后,Farmer John打算用他在MP3播放器市场新买的iCow来听些音乐,放松一下.FJ的iCow里存了N(1 <= N < ...

  3. 洛谷——P2381 圆圆舞蹈

    P2381 圆圆舞蹈 题目描述 熊大妈的乃修在时针的带领下,围成了一个圆圈舞蹈,由于没有严格的教育,奶牛们之间的间隔不一致. 奶牛想知道两只最远的奶牛到底隔了多远.奶牛A到B的距离为A顺时针走和逆时针 ...

  4. 51nod 1052 (dp)

    最大M子段和 N个整数组成的序列a[1],a[2],a[3],…,a[n],将这N个数划分为互不相交的M个子段,并且这M个子段的和是最大的.如果M >= N个数中正数的个数,那么输出所有正数的和 ...

  5. Codeforces Round #426 (Div. 2) D The Bakery(线段树 DP)

     The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard input ...

  6. 【BZOJ 3924】【ZJOI 2015】幻想乡战略游戏

    http://www.lydsy.com/JudgeOnline/problem.php?id=3924 gty的测试题,不会动态点分治而且看不出来链剖做法而且暴力打残所以这道题喜闻乐见的爆零了qwq ...

  7. ARC 080 - C 4-adjacent

    题面在这里! 把每个数替换成它在2上的指数,然后发现0只能和>=2的相邻,所以手玩一下就好啦. #include<bits/stdc++.h> #define ll long lon ...

  8. Codeforces 961E 主席树

    题意: 给出一个n个数的序列,求有几对(i,j)满足a[i]>=j&&a[j]>=i,(i,j)和(j,i)只能算一对. 考虑第i个数会有几个j(j<i)满足条件,首 ...

  9. 【NTT】hdu1402 A * B Problem Plus

    r·2^k+1 r k g 3 1 1 2 5 1 2 2 17 1 4 3 97 3 5 5 193 3 6 5 257 1 8 3 7681 15 9 17 12289 3 12 11 40961 ...

  10. 【状压DP】BZOJ2734-[HNOI2012]集合选数

    已经八月份了药丸,开始肝作业并且准备高考啦!! [题目大意] <集合论与图论>这门课程有一道作业题,要求同学们求出{1, 2, 3, 4, 5}的所有满足以 下条件的子集:若 x 在该子集 ...