TouchJSON是OC转换JSON的一个第三方类库,使用简单。在GitHub上可以轻松获取:TouchJSON

以下是简单的事例演示从douban movie请求JSON数据并转为字典对象后对控件赋值。

#import "ViewController.h"

#import "CJSONSerializer.h"

#import "CJSONDeserializer.h"

#import "NSDictionary_JSONExtensions.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UITextView *textView;

@property (nonatomic, strong) NSMutableDictionary *dic;

@property (nonatomic, strong) NSString *text;

@end
@implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
} - (IBAction)didClickTouchJSONButton:(id)sender {
//GCD异步
dispatch_queue_t q1 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_async(q1, ^{
NSURL *url = [NSURL URLWithString:@"https://api.douban.com/v2/movie/subject/22265299"];
NSString *jsonString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil]; //直接转换,import CJSONDeserializer.h
//self.dic = [[CJSONDeserializer deserializer] deserialize:[jsonString dataUsingEncoding:NSUTF8StringEncoding] error:nil];
//如果使用下面扩展了NSDictionary的方法,请import NSDictionary_JSONExtensions.h
self.dic = [NSDictionary dictionaryWithJSONString:jsonString error:nil];
NSString *title = [self.dic objectForKey:@"original_title"];
NSMutableArray *genresArray = [self.dic objectForKey:@"genres"];
NSString *genres = [NSString stringWithFormat:@"%@/%@",[genresArray objectAtIndex:0],[genresArray objectAtIndex:1]];
NSString *summary = [self.dic objectForKey:@"summary"];
self.text = [NSString stringWithFormat:@"电影名称:%@\n体裁:%@\n剧情介绍:%@",title,genres,summary];
//更新UI操作需要在主线程
dispatch_async(dispatch_get_main_queue(), ^{
self.textView.text = self.text;
});
});
}

最新文章

  1. web前端基础知识-(五)jQuery
  2. 简易版C语言程序设计语法
  3. Windows WMIC命令使用详解
  4. 【问题排查】StringIndexOutOfBoundsException
  5. iOS 开发笔记 -- 各种细枝末节的知识(水滴石穿)
  6. Mybatis 开启事务@Transactional
  7. iOS界面的绘制和渲染
  8. 三、 将DataTable 转换为List
  9. if最简单的用法
  10. 为智能硬件提供一站式解决方案——机智云GoKit评测
  11. XMPP 测试工具
  12. 使用Apache JMeter对SQL Server、Mysql、Oracle压力测试(三)
  13. Tesseract识别图片提取文字&字库训练
  14. 关于如何使`(a === 1 && a === 2 && a === 3)`返回`true`问题的思考
  15. Get and Set Column/Row Names for Data Frames
  16. Qt贴图实现地图标记效果
  17. 谷歌商店高级搜索 Google play advanced search
  18. iOS设备分辨率
  19. MJRefresh原理分析
  20. RESTful源码笔记之RESTful Framework的APIview, Viewset总结分析

热门文章

  1. UVa 10382 喷水装置(贪心)
  2. Codeforces Beta Round #94 div2 D 优先队列
  3. Qt_QString::split测试
  4. 《剑指offer》第二十一题(调整数组顺序使奇数位于偶数前面)
  5. Codeforces 862B - Mahmoud and Ehab and the bipartiteness
  6. Hibernate实例
  7. 3-11 《Ruby元编程》第4章block块 3-12
  8. mysql查询出来的某一列合并成一个字段
  9. python-day64--web框架
  10. 破解VS