码迷,mamicode.com
首页 > Web开发 > 详细

JSON解析 实现界面 数据分离.

时间:2014-07-06 18:09:56      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   java   使用   strong   

JSON

作为一种轻量级的数据交换格式,正在逐步取代XML,成为网络数据的通用格式

基于JavaScript的一个子集

易读性略差,编码手写难度大,数据量小

JSON格式取代了XML给网络传输带来了很大的便利,但是却没有了XML的一目了然,尤其是JSON数据很长的时候,我们会陷入繁琐复杂的数据节点查找中

JSON格式说明

对象

{}

格式 {key : value, key : value,...} 的键值对的结构

可以反序列化为OC中的NSDictionary

数组

[]

格式 ["java","javascript","vb",...]

可以反序列化为OC中的NSArray

提示

JSON的数据格式与OC中的快速包装方法非常类似

JSON的数据格式同样支持嵌套

 

 

解析服务器端返回JSON数据

 

从iOS 5开始,使用NSJSONSerialization对JSON解析

 

其他常见的三种JSON解析第三方库:

SBJson 因为API简单易用,可能还会有一些应用中留存

JSONKit JSONKit的开发者称:JSONKit的性能优于苹果

TouchJson

 

 

JSON的序列化和反序列化

反序列化

[NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];

 

序列化

[NSJSONSerialization dataWithJSONObject:array options:0 error:NULL];

 

NSJSONReadingOptions

NSJSONReadingMutableContainers = 1,    根节点可变

NSJSONReadingMutableLeaves = 2,          节点可变

NSJSONReadingAllowFragments = 4,         根节点可以不是NSDictionary或者NSArray

 

 

 

 

//代码解析步骤

#import <Foundation/Foundation.h>

 

@interface Video : NSObject

 

@property (nonatomic, copy) NSNumber *videoId;

@property (nonatomic, copy) NSString *name;

@property (nonatomic, copy) NSNumber *length;

@property (nonatomic, copy) NSString *videoURL;

@property (nonatomic, copy) NSString *imageURL;

@property (nonatomic, copy) NSString *desc;

 

- (instancetype)initWithDict:(NSDictionary *)dict;

+ (instancetype)videoWithDict:(NSDictionary *)dict;

 

@end

 

 

#import "Video.h"

@implementation Video

 

- (instancetype)initWithDict:(NSDictionary *)dict

{

    self = [super init];

    if (self) {

        [self setValuesForKeysWithDictionary:dict];

    }

    return self;

}

 

+ (instancetype)videoWithDict:(NSDictionary *)dict

{

    return [[self alloc] initWithDict:dict];

}

 

 

 

@end

 

 

 

//解析步骤

#import "PLMJSONViewController.h"

#import "Video.h"

 

@interface PLMJSONViewController ()

 

@end

 

@implementation PLMJSONViewController

 

/** 重写父类的加载数据方法 */

- (void)loadData

{

    NSLog(@"%s", __func__);

    

    // 1. url

    NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/videos.json"];

    

    // 2. request

    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    

    // 3. 发送异步请求

    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

        

        // data是一个json数据

        // 对data进行反序列化,解析(很多时候我们解析出来的是字典)

        NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];

        

        // 建立视频的数组

        NSMutableArray *arrayM = [NSMutableArray array];

        

        for (NSDictionary *dict in array) {

            [arrayM addObject:[Video videoWithDict:dict]];

        }

        

        // 刷新数据,更新UI

        dispatch_async(dispatch_get_main_queue(), ^{

            self.dataList = arrayM;

        });

    }];

}

 

@end

 

#import <UIKit/UIKit.h>

 

@interface PLMViewController : UITableViewController

 

@property (nonatomic, strong) NSArray *dataList;

 

/** 加载网络数据 */

- (void)loadData;

 

@end

 

#import "PLMViewController.h"

#import "Video.h"

 

@interface PLMViewController ()

 

@end

 

@implementation PLMViewController

 

/** 设置表格的数组数据 */

- (void)setDataList:(NSArray *)dataList

{

    _dataList = dataList;

    

    // 表格的数据源,绑定在dataList,当重新设置了dataList的内容,需要刷新表格

    [self.tableView reloadData];

    

    // 停止刷新控件

    [self.refreshControl endRefreshing];

}

 

- (void)viewDidLoad

{

    [super viewDidLoad];

    

    // 加载网络数据

    [self loadData];

}

 

- (IBAction)refresh

{

    [self loadData];

}

 

- (void)loadData

{

    // 此方法的具体实现在子类中

}

 

#pragma mark - 表格的数据源方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return self.dataList.count;

}

 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *ID = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];

    

    Video *v = self.dataList[indexPath.row];

    cell.textLabel.text = v.name;

    

    return cell;

}

 

@end

 

JSON解析 实现界面 数据分离.,布布扣,bubuko.com

JSON解析 实现界面 数据分离.

标签:des   style   http   java   使用   strong   

原文地址:http://www.cnblogs.com/penglimin/p/3825041.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!