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