標籤: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