网络数据解析之 JSON 解析

2016-07-03  本文已影响74人  箫声_筱昇

解析一下.json文档

[
 {
 "name":"张三",
 "gender":"男",
 "age":"18"
 },
 {
 "name":"李四",
 "gender":"男",
 "age":"28"
 },
 {
 "name":"王六",
 "gender":"女",
 "age":"38"
 }
 ]
//student.h
#import <Foundation/Foundation.h>

@interface Student : NSObject
@property (nonatomic, retain) NSString *name,*age,*gender;
- (instancetype)initWithDictionary:(NSDictionary *)otherDictionary;

@end
//student.m
#import "Student.h"

@implementation Student

- (instancetype)initWithDictionary:(NSDictionary *)otherDictionary{
    if (self = [super init]) {
        [self setValuesForKeysWithDictionary:otherDictionary];//使用 kvc 对属性赋值.
    }
    return self;
}


- (NSString *)description
{
    return [NSString stringWithFormat:@"name:%@,gender:%@,age:%@", self.name,self.gender,self.age];
}

@end
//viewController.h
#import "ViewController.h"
#import "Student.h"
@interface ViewController ()

@end

使用系统提供的类来进行 json 解析

//使用系统提供的类进行 json 数据解析
- (IBAction)parserJSON:(id)sender {
    //json文件的路径,
   NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"json"];
    //提取内容
    NSData *data = [[NSData alloc] initWithContentsOfFile:filePath];
    NSError *error = nil;
    
    NSMutableArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    //判断是否出现问题
    if (error == nil) {
        NSLog(@"%@",array);
    }else{
        NSLog(@"%@",error);
    }
    
    
    NSMutableArray  *studentArray = [[NSMutableArray alloc] initWithCapacity:1];
    
    for (NSDictionary *dic in array) {
        Student *student = [[Student alloc] initWithDictionary:dic];
        [studentArray addObject:student];
    }
    for (Student *stu in studentArray) {
        NSLog(@"%@",stu);
    }
}
//解析的结果为
2016-07-03 19:15:19.521 jsonParser[2147:180052] (
        {
        age = 18;
        gender = "\U7537";
        name = "\U5f20\U4e09";
    },
        {
        age = 28;
        gender = "\U7537";
        name = "\U674e\U56db";
    },
        {
        age = 38;
        gender = "\U5973";
        name = "\U738b\U516d";
    }
)
2016-07-03 19:15:19.522 jsonParser[2147:180052] name:张三,gender:男,age:18
2016-07-03 19:15:19.522 jsonParser[2147:180052] name:李四,gender:男,age:28
2016-07-03 19:15:19.522 jsonParser[2147:180052] name:王六,gender:女,age:38

使用第三方 JSONKit 来进行解析,首先要下载有关的 JSONKit 的. h 和. m 文件
然后

//使用第三方 jsonkit 进行 json数据解析.这样做的好处就是可以清楚的观察到解析的数据.
- (IBAction)patserJSONKIT:(id)sender {
    //获取json文件的路径
    NSString *filepath = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"json"];
    //获取 json 文件的内容
    NSString *contentString = [[NSString alloc] initWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:nil];
    //使用 jsonkit进行解析
    NSArray *array = [contentString objectFromJSONString];
    NSLog(@"%@",array);
    //数据封装
    NSMutableArray *studentArray = [[NSMutableArray alloc] initWithCapacity:3];

    for (NSDictionary *dic in array) {
        Student *student = [[Student alloc] initWithDictionary:dic];
        [studentArray addObject:student];
    }
    for (Student *stu in studentArray) {
        NSLog(@"%@",stu);
    }
}
//解析的结果为
2016-07-03 19:17:45.691 jsonParser[2147:180052] (
        {
        age = 18;
        gender = "\U7537";
        name = "\U5f20\U4e09";
    },
        {
        age = 28;
        gender = "\U7537";
        name = "\U674e\U56db";
    },
        {
        age = 38;
        gender = "\U5973";
        name = "\U738b\U516d";
    }
)
2016-07-03 19:17:45.691 jsonParser[2147:180052] name:张三,gender:男,age:18
2016-07-03 19:17:45.691 jsonParser[2147:180052] name:李四,gender:男,age:28
2016-07-03 19:17:45.691 jsonParser[2147:180052] name:王六,gender:女,age:38
上一篇下一篇

猜你喜欢

热点阅读