IOS Objective-C JSON Parse轉成Dictionary

IOS5後支援的功能

  1. Parse json object into a dic  
  2.   
  3. - ( void ) fetchedData: (NSData *) responseData { / / Parse out the JSON dataNSError * Error;  
  4.     NSDictionary * json = [NSJSONSerialization   
  5.         JSONObjectWithData: responseData  / / 1  
  6.    
  7.         options: kNilOptions   
  8.         error: & error];  
  9.    
  10.     NSArray * latestLoans = [JSON objectForKey: @ "loans" ];  / / 2  
  11.    
  12.     NSLog (@ "loans:% @" , latestLoans);  / / 3  
  13. }  
  14.   
  15. Generate json string to object  
  16. / / Build an info object and convert to json  
  17. NSDictionary * info = [NSDictionary dictionaryWithObjectsAndKeys: [Loan objectForKey: @ "name" ],   
  18.     @ "who" ,  
  19.   [(NSDictionary *) [Loan objectForKey: @ "location" ]   
  20.     objectForKey: @ "Country" ],   
  21.     @ "where" ,  
  22.   [NSNumber numberWithFloat: outstandingAmount],   
  23.     @ "what" ,  
  24.   nil];  
  25.    
  26. / / Convert object to data  
  27. NSData * jsonData = [NSJSONSerialization dataWithJSONObject: info   
  28.   options: NSJSONWritingPrettyPrinted error: & error];  
  29.   
  30. / / Print out the data contents  
  31. jsonSummary.text = [[NSString alloc] initWithData: jsonData                                          
  32.   encoding: NSUTF8StringEncoding];  
  33.   
  34. Add json method to dic  
  35. @ InterfaceNSDictionary (JSONCategories)  
  36. + (NSDictionary *) dictionaryWithContentsOfJSONURLString: (NSString *) urlAddress;  
  37. - (NSData *) toJSON;  
  38. @ End  
  39.    
  40. @ ImplementationNSDictionary (JSONCategories)  
  41. + (NSDictionary *) dictionaryWithContentsOfJSONURLString: (NSString *) urlAddress {  
  42.     NSData * data = [NSData dataWithContentsOfURL: [NSURL URLWithString: urlAddress]];  
  43.     __autoreleasing NSError * error = nil;  
  44.     id result = [NSJSONSerialization JSONObjectWithData: data   
  45.       options: kNilOptions error: & error];  
  46.     if (Error! = nil) returnnil;  
  47.     return  result;  
  48. }  
  49.    
  50. - (NSData *) toJSON {  
  51.     NSError * error = nil;  
  52.     id result = [NSJSONSerialization dataWithJSONObject: self   
  53.       options: kNilOptions error: & error];  
  54.     if (Error! = nil) returnnil;  
  55.     return  result;      
  56. @ End  
  57.   
  58. Use Liezi  
  59. NSDictionary * MyInfo = [NSDictionary dictionaryWithContentsOfJSONURLString: @ "http://www.yahoo.com/news.json" ];  
  60. NSDictionary * information = [NSDictionary dictionaryWithObjectsAndKeys: @ "Orange" , @ "Apple" , @ "Banana" , @ "fig" , nil];  
  61. NSData * json = [information toJSON];  
  62.   
  63. Determine whether the json of  
  64.   
  65. BOOL isTurnableToJSON = [NSJSONSerialization isValidJSONObject: object] 

張貼留言

Featured Post

安裝/更新 AZURE POWERSHELL

以下皆為powershell 安裝的指令 Install-Module Azure OR Install-Module Azure -AllowClobber 更新的指令 先看舊的版本 Get-Module AzureRM -ListAvaila...

Popular Posts