iOS 假数据,json数据
2017-05-10 本文已影响753人
zero_zql
iOS 假数据,json数据
通常在开发阶段经常需要模拟一些数据来进行布局,这里就跟大家分享下自己的方式
首先我们建立一个txt文本文件,将你的json数据直接放入其中,再引入到你的项目中,一下代码就是将文本数据提取的方法:
NSError *error;
NSString *dataStr = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"json" ofType:@"txt"] encoding:NSUTF8StringEncoding error:&error];
NSData *jsonData = [dataStr dataUsingEncoding:NSUTF8StringEncoding];
NSError *jerror;
NSDictionary*dic = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&jerror];
下方为一个json字符串
{"status":1,"message":"获取成功","result":[{"cook_id":8,"name":"测试菜谱3","abstract":"这是摘要","image_id_cover":"http:\/\/360tjy-health.oss-cn-shanghai.aliyuncs.com\/image\/201705\/f20474dac91b4c1d2f27c46458905e8c.png","reading_number":133,"like_number":1,"is_yun":1},{"cook_id":85,"name":"测试菜谱2","abstract":"朝伟","image_id_cover":"http:\/\/360tjy-health.oss-cn-shanghai.aliyuncs.com\/image\/201705\/894f9ad22a2d78ccbd6eb3b2e1978acb.png","reading_number":184,"like_number":2,"is_yun":1},{"cook_id":86,"name":"测试菜谱1","abstract":"测试菜谱摘要","image_id_cover":"http:\/\/360tjy-health.oss-cn-shanghai.aliyuncs.com\/image\/201705\/cd9f9968e2e2c6f75b5855529e388e78.jpg","reading_number":41,"like_number":0,"is_yun":1}],"total_num":3}