您的位置:首页 > 产品设计 > UI/UE

UI - JSONParser

2015-10-18 15:31 519 查看
<ViewController.h>

</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


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: