服務器返回給客戶端的數據,一般都是JSON格式或者XML格式(文件下載除外),JSON和XML的比較這里不詳述,可以參考這文章[http://www.cnblogs.com/SanMaoSpace/p/3139186.html](http://www.cnblogs.com/SanMaoSpace/p/3139186.html)?。 ?總的來說XML文件龐大,文件格式復雜,解析需要花費較多的資源和時間,傳輸占帶寬。JSON數據格式比較簡單,易于讀寫,格式都是壓縮的,占用帶寬小,移動開發首選。
JSON:
1、JSON的格式很像OC中的字典和數組
~~~
{"name" : "jack", "age" : 10}
{"names" : ["jack", "rose", "jim"]}
~~~
標準JSON格式的注意點:key必須用雙引號
JSON – OC 轉換對照表:

2、在iOS中,JSON的常見解析方案有4種
第三方框架:JSONKit、SBJson、TouchJSON(性能從左到右,越差)?
蘋果原生(自帶):NSJSONSerialization(性能最好)
NSJSONSerialization的常見方法:
?2.1、JSON 轉 OC對象(其中的過程是json轉換為字典,字典再轉換為對象)
`+ (id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError **)error;`
例子: 請求服務器,返回json數據。json數據封裝到數組對象中。
服務器返回數據json格式:

~~~
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:@"http://localhost:8080/myService/video"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
if (connectionError || data==nil) {
[MBProgressHUD showError:@"網絡繁忙,請稍后再試!"];
return ;
}
//json 轉化為data 得到字典
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
//取出字典中的某一個key
NSArray *videoArray = dict[@"videos"];
//字典轉模型
for (NSDictionary *videoDict in videoArray) {
//字典轉模型(對象)
Video *video = [Video videoWithDict:videoDict];
[self.videos addObject:video];
}
}];
}
~~~
?2.2、OC對象 轉JSON數據 (對象轉字典,字典再轉json)
+ (NSData *)dataWithJSONObject:(id)obj options:(NSJSONWritingOptions)opt error:(NSError **)error;
~~~
#import "ViewController.h"
#import "Person.h"
@interface ViewController ()
@property(nonatomic,strong) Person *person;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_person = [[Person alloc]init];
_person.name = @"kobe";
_person.age = 24;
_person.sex = @"男";
_person.phone = @"1112334444";
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//person 轉字典
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[@"age"] = [NSString stringWithFormat:@"%d",self.person.age];
dict[@"name"] = self.person.name;
dict[@"sex"] = self.person.sex;
dict[@"phone"] = self.person.phone;
NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
NSString *dataStr = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",dataStr);
}
@end
~~~
結果:
~~~
?{
? "age" : "24",
? "sex" : "男",
? "phone" : "1112334444",
? "name" : "kobe"
}
~~~
NSJSONReadingOptions枚舉:
NSJSONReadingMutableContainers:返回可變容器,NSMutableDictionary或NSMutableArray。?
NSJSONReadingMutableLeaves:返回的JSON對象中字符串的值為NSMutableString?
NSJSONReadingAllowFragments:允許JSON字符串最外層既不是NSArray也不是NSDictionary,但必須是有效的JSON Fragment。
NSJSONWritingPrettyPrinted:的意思是將生成的json數據格式化輸出,這樣可讀性高,不設置則輸出的json字符串就是一整行。
- 前言
- iOS開發實踐之SQLite3
- iOS開發實踐之FMDB
- Obj-C與javascript交互之WebViewJavascriptBridge
- iOS開發實踐之UIWebView
- iOS開發實踐之多線程(基本概念)
- iOS開發實踐之多線程(NSThread)
- iOS開發實踐之多線程(GCD)
- iOS開發實踐之多線程(單例模式)
- iOS開發實踐之xib加載注意問題
- iOS開發實踐之多線程(NSOperation)
- iOS開發實踐之cell下載圖片(NSOperation)
- iOS開發實踐之cell下載圖片(自定義NSOperation)
- iOS開發實踐之cell下載圖片(SDWebImage)
- iOS開發實踐之JSON
- iOS開發實踐之XML
- iOS開發實踐之GET和POST請求
- iOS開發實踐之網絡檢測Reachability
- iOS開發實踐之MD5加密