Objective-C Json 使用,objective-cjson

來源:互聯網
上載者:User

Objective-C Json 使用,objective-cjson

Objective-c json

通過使用NSJSONSerialization 可以Json與Foundation的相互轉換。下面具體介紹 Objective-c json 的使用。

Json To Fundation

使用 JSONObjectWithData 可以將 Json 轉化為 Foundation。Json的頂層可以是{} 或 []因此可以有 NSDictionary 和 NSArray 兩種格式。讀取使用 ObjectForKey 返回對應的對象。

123456789101112131415161718192021222324252627282930313233 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 轉換為 Json。其中 options:NSJSONWritingPrettyPrinted 是分行輸出json ,無空格輸出使用 option:kNilOptions。

下面這段代碼是IOS內購擷取商品列表。擷取後,將內容添加到Json中。

1234567891011121314151617181920212223242526272829303132333435 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.");}        


本文出自: [ 松陽的部落格 ] / [ blog.csdn.net/fansongy ] 禁止用於商業用途 轉載請註明出處

原文連結: http://www.songyang.net/objective-c-json/


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.