UI - JSONParser
2015-10-18 15:31
519 查看
<ViewController.h>
<ViewController.m>
<Student.h>
<Student.m>
</pre><pre name="code" class="objc">#import <UIKit/UIKit.h> @interface ViewController : UIViewController @end
<ViewController.m>
#import "ViewController.h" #import "Student.h" #import "JSONKit.h" @interface ViewController () @end @implementation ViewController - (IBAction)PublicEvaluation:(id)sender { //1.获取文件路径 NSString *filePath = [[NSBundle mainBundle]pathForResource:@"数据" ofType:@"json"]; //2.根据文件路径获取数据 NSData *data = [NSData dataWithContentsOfFile:filePath]; //3.解析 NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; NSLog(@"%@",dataDic); //获取"苏州桥" 的字符串 //1.获取数组 NSArray *business = [dataDic valueForKey:@"businesses"]; //2.获取数组中的字典 NSDictionary *dic1 = [business firstObject]; //3.获取数组 NSArray *regions =[dic1 valueForKey:@"regions"]; //4.获取目标数据 NSString *str = [regions lastObject]; NSLog(@"%@",str); } //JSONKit 是一个 JSON解析工具,它的效率仅次于系统的 JSON 解析方式,它是通过给 NSString NSData 添加分类的方式进行数据解析 //JSONKit - (IBAction)JSONKitParser:(id)sender { //NSString // //1.获取文件路径 // NSString *filePath = [[NSBundle mainBundle]pathForResource:@"Student" ofType:@"json"]; // //2.通过文件路径获取字符串 // NSString *jsonString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]; // //3.解析 // NSArray *dataArr = [jsonString objectFromJSONString]; // //4.遍历 // for (NSDictionary *dic in dataArr) { // NSLog(@"%@",[dic valueForKey:@"name"]); // //封装对象 // Student *stu = [[Student alloc]init]; // [stu setValuesForKeysWithDictionary:dic]; // NSLog(@"%@",stu.name); // } //NSSata //1.获取文件路径 NSString *filePath = [[NSBundle mainBundle]pathForResource:@"Student" ofType:@"json"]; //2.获取 NSData NSData *jsonData = [NSData dataWithContentsOfFile:filePath]; //3.解析 NSArray *arr = [jsonData objectFromJSONData]; NSLog(@"%@",arr); } //NSJSONSerialization解析json,NSJSON只支持解析UTF8编码的数据(还有UTF-16LE之类的,都不常用),所以要先把返回的数据转成UTF8格式。这里会尝试用HTTP返回的编码类型和自己设置的stringEncoding去把数据解码转成字符串NSString,再把NSString用UTF8编码转成NSData,再用NSJSONSerialization解析成对象返回。 //System - (IBAction)SystemParser:(id)sender { //1.获取文件路径 NSString *filePath = [[NSBundle mainBundle]pathForResource:@"Student" ofType:@"json"]; //2.获取数据 NSData *data = [NSData dataWithContentsOfFile:filePath]; //3.解析 NSMutableArray *arr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; NSLog(@"%@",arr); } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
<Student.h>
#import <Foundation/Foundation.h> @interface Student : NSObject @property (nonatomic,copy)NSString *name; @property (nonatomic,copy)NSString *sex; @property (nonatomic,copy)NSString *age; @end
<Student.m>
#import <Foundation/Foundation.h> @interface Student : NSObject @property (nonatomic,copy)NSString *name; @property (nonatomic,copy)NSString *sex; @property (nonatomic,copy)NSString *age; @end
相关文章推荐
- HibernateQueryException: ClassNotFoundException: org.hibernate.hql.ast.HqlToken
- 关于LigerUI表单元素的动态替换
- UI - XMLParser
- 【IOS 开发学习总结-OC-55】故事板中 segue 的 push、modal、popover、replace、custom 学习笔记
- UI - Block
- C#中的string与stringbuilder有什么区别
- mysql uuid 性能调优
- String,StringBuffer与StringBuilder的区别
- EasyUI+js实现动态加载导航栏
- IOS基础UI之(六)综合小案例-应用管理
- UI - UITabBarController
- iOS改变UITextField中光标颜色
- org.hibernate.exception.SQLGrammarException: could not execute query错误解决笔记
- UIScrollViewA都PI得知。
- AutoLayout自定义tableViewCell --- Masonry + UITableView+FDTemplateLayoutCell 纯代码实现
- UGUI 里特效的层级
- 第十四篇:cxcore100.dll cv100.dll highgui100.dll libguide40.dll 的驱动安装
- hdu(5504)——GT and sequence
- php开发日记之使用ueditor上传文件至云存储
- 1696: [Usaco2007 Feb]Building A New Barn新牛舍