Objective-C Json 實(shí)例詳解
Objective-C Json 實(shí)例詳解
通過(guò)使用NSJSONSerialization 可以Json與Foundation的相互轉(zhuǎn)換。下面具體介紹 Objective-c json 的使用。
Json To Fundation
使用 JSONObjectWithData 可以將 Json 轉(zhuǎn)化為 Foundation。Json的頂層可以是{} 或 []因此可以有 NSDictionary 和 NSArray 兩種格式。讀取使用 ObjectForKey 返回對(duì)應(yīng)的對(duì)象。
NSString* items = @"{"items":["item0","item1","item2"]}";
NSData *data= [items dataUsingEncoding:NSUTF8StringEncoding];
NSError *error = nil;
id jsonObject = [NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingAllowFragments
error:&error];
if ([jsonObject isKindOfClass:[NSDictionary class]]){
NSDictionary *dictionary = (NSDictionary *)jsonObject;
NSLog(@"Dersialized JSON Dictionary = %@", dictionary);
}else if ([jsonObject isKindOfClass:[NSArray class]]){
NSArray *nsArray = (NSArray *)jsonObject;
NSLog(@"Dersialized JSON Array = %@", nsArray);
} else {
NSLog(@"An error happened while deserializing the JSON data.");
}
NSDictionary *dict = (NSDictionary *)jsonObject;
NSArray* arr = [dict objectForKey:@"items"];
NSLog(@"list is %@",arr);
Fundation To Json
使用 dataWithJsonObject 可以將 Fundation 轉(zhuǎn)換為 Json。其中 options:NSJSONWritingPrettyPrinted 是分行輸出json ,無(wú)空格輸出使用 option:kNilOptions。
下面這段代碼是IOS內(nèi)購(gòu)獲取商品列表。獲取后,將內(nèi)容添加到Json中。
NSArray *myProduct = response.products;
NSDictionary *myDict;
NSMutableDictionary *dict = [NSMutableDictionary
dictionaryWithCapacity: 4];
for(int i = 0;i<myProduct.count;++i)
{
//NSLog(@"----------------------");
//NSLog(@"Product title: %@" ,[myProduct[i] localizedTitle]);
//NSLog(@"Product description: %@" ,[myProduct[i] localizedDescription]);
//NSLog(@"Product price: %@" ,[myProduct[i] price]);
//NSLog(@"Product id: %@" ,[myProduct[i] productIdentifier]);
myDict = [NSDictionary dictionaryWithObjectsAndKeys:
[myProduct[i] localizedTitle], @"title",
[myProduct[i] localizedDescription], @"desc",
[myProduct[i] price], @"price",
[myProduct[i] productIdentifier], @"product", nil];
[dict setValue: myDict forKey: [myProduct[i] productIdentifier]];
}
if([NSJSONSerialization isValidJSONObject:dict])
{
NSError* error;
NSData *str = [NSJSONSerialization dataWithJSONObject:dict
options:kNilOptions error:&error];
NSLog(@"Result: %@",[[NSString alloc]initWithData:str
encoding:NSUTF8StringEncoding]);
}
else
{
NSLog(@"An error happened while serializing the JSON data.");
}
如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
MacOS無(wú)法掛載NFS Operation not permitted錯(cuò)誤解決辦法
這篇文章主要介紹了MacOS無(wú)法掛載NFS Operation not permitted錯(cuò)誤解決辦法的相關(guān)資料2017-02-02
iOS 解決UICollectionView 計(jì)算 Cell 大小的問(wèn)題
本文主要介紹iOS UICollectionView,這里給大家一個(gè)實(shí)例代碼作為參考,并指出經(jīng)常遇到的問(wèn)題和解決辦法,希望能幫助有需要的小伙伴2016-07-07
iOS開(kāi)發(fā)教程之APP內(nèi)部切換語(yǔ)言的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于iOS開(kāi)發(fā)教程之APP內(nèi)部切換語(yǔ)言的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02
iOS端React Native差異化增量更新的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于iOS端React Native差異化增量更新的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-06-06
淺談Unity中IOS Build Settings選項(xiàng)的作用
下面小編就為大家分享一篇淺談Unity中IOS Build Settings選項(xiàng)的作用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
iOS啟動(dòng)頁(yè)倒計(jì)時(shí)跳過(guò)按鈕功能
這篇文章主要介紹了iOS啟動(dòng)頁(yè)倒計(jì)時(shí)跳過(guò)按鈕功能,需要的朋友可以參考下2017-07-07
IOS實(shí)現(xiàn)視頻動(dòng)畫(huà)效果的啟動(dòng)圖
這篇文章實(shí)現(xiàn)的是一個(gè)關(guān)于啟動(dòng)頁(yè)或者引導(dǎo)頁(yè)的視頻動(dòng)畫(huà)效果的實(shí)現(xiàn)過(guò)程,對(duì)于大家開(kāi)發(fā)APP具有一定的參考借鑒價(jià)值,有需要的可以來(lái)看看。2016-09-09

