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)文章
IOS 中 new 和 alloc init 的對(duì)比
這篇文章主要介紹了IOS 中 new 和 alloc init 的區(qū)別的相關(guān)資料,需要的朋友可以參考下2017-02-02iOS模仿QQ側(cè)邊欄的實(shí)現(xiàn)方法實(shí)例
項(xiàng)目中要做側(cè)邊欄效果,網(wǎng)上諸多demo,都不是最理想的。最后決定自己來(lái)實(shí)現(xiàn)一個(gè),所以下面這篇文章主要給大家介紹了關(guān)于利用iOS模仿QQ側(cè)邊欄的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2017-12-12淺談iOS推送證書(shū)生成pem文件(詳細(xì)生成過(guò)程)
這篇文章主要介紹了淺談iOS推送證書(shū)生成pem文件(詳細(xì)生成過(guò)程),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08iOS11實(shí)現(xiàn)App內(nèi)自動(dòng)連接Wi-Fi的方法
這篇文章主要給大家介紹了關(guān)于iOS11實(shí)現(xiàn)App內(nèi)自動(dòng)連接Wi-Fi的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10iOS中的UITableView的重用機(jī)制與加載優(yōu)化詳解
本篇文章主要介紹了iOS中的UITableView的重用機(jī)制與加載優(yōu)化詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02