最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Objective-C Json 實(shí)例詳解

 更新時(shí)間:2017年10月29日 10:23:28   作者:松陽(yáng)  
這篇文章主要介紹了 Objective-C Json 實(shí)例詳解的相關(guān)資料,希望通過(guò)本文能幫助到大家,讓大家掌握Object-C Json的使用,需要的朋友可以參考下

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)文章

最新評(píng)論

宁安市| 松桃| 萝北县| 霍城县| 资溪县| 台前县| 靖边县| 翼城县| 柳河县| 高淳县| 顺昌县| 永丰县| 梅州市| 金堂县| 屏东县| 西畴县| 长沙市| 汝阳县| 象山县| 澳门| 开远市| 修水县| 宁城县| 湄潭县| 潜山县| 塘沽区| 天柱县| 舞钢市| 津南区| 长武县| 吴川市| 化德县| 和硕县| 南澳县| 霍林郭勒市| 徐闻县| 隆林| 江安县| 永定县| 武隆县| 木兰县|