国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

iOSJSON解析

2019-11-14 19:55:55
字體:
供稿:網(wǎng)友

解析json成dic對象  

  •   
  • -(void)fetchedData:(NSData*)responseData {//parse out the json dataNSError* error;  
  •     NSDictionary* json =[NSJSONSerialization   
  •         JSONObjectWithData:responseData //1  
  •    
  •         options:kNilOptions   
  •         error:&error];  
  •    
  •     NSArray* latestLoans =[json objectForKey:@"loans"]; //2  
  •    
  •     NSLog(@"loans: %@", latestLoans); //3  
  • }  
  •   
  • 把對象生成json string  
  • //build an info object and convert to json  
  • NSDictionary* info =[NSDictionary dictionaryWithObjectsAndKeys:[loan objectForKey:@"name"],   
  •     @"who",  
  •   [(NSDictionary*)[loan objectForKey:@"location"]   
  •     objectForKey:@"country"],   
  •     @"where",  
  •   [NSNumber numberWithFloat: outstandingAmount],   
  •     @"what",  
  •   nil];  
  •    
  • //convert object to data  
  • NSData* jsonData =[NSJSONSerialization dataWithJSONObject:info   
  •   options:NSJSONWritingPRettyPrinted error:&error];  
  •   
  • //print out the data contents  
  • jsonSummary.text =[[NSString alloc] initWithData:jsonData                                          
  •   encoding:NSUTF8StringEncoding];  
  •   
  • 添加json方法至dic  
  • @interfaceNSDictionary(JSONCategories)  
  • +(NSDictionary*)dictionaryWithContentsOfJSONURLString:(NSString*)urlAddress;  
  • -(NSData*)toJSON;  
  • @end  
  •    
  • @implementationNSDictionary(JSONCategories)  
  • +(NSDictionary*)dictionaryWithContentsOfJSONURLString:(NSString*)urlAddress{  
  •     NSData* data =[NSData dataWithContentsOfURL:[NSURL URLWithString: urlAddress]];  
  •     __autoreleasing NSError* error =nil;  
  •     id result =[NSJSONSerialization JSONObjectWithData:data   
  •       options:kNilOptions error:&error];  
  •     if(error !=nil)returnnil;  
  •     return result;  
  • }  
  •    
  • -(NSData*)toJSON{  
  •     NSError* error =nil;  
  •     id result =[NSJSONSerialization dataWithJSONObject:self   
  •       options:kNilOptions error:&error];  
  •     if(error !=nil)returnnil;  
  •     return result;      
  • }@end  
  •   
  • 使用列子  
  • NSDictionary* myInfo =[NSDictionary dictionaryWithContentsOfJSONURLString:@"http://www.yahoo.com/news.json"];  
  • NSDictionary* information =[NSDictionary dictionaryWithObjectsAndKeys:@"orange",@"apple",@"banana",@"fig",nil];  
  • NSData* json =[information toJSON];  
  •   
  • 判斷是否可json化  
  •   
  • BOOL isTurnableToJSON =[NSJSONSerialization isValidJSONObject: object]  

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 新巴尔虎左旗| 犍为县| 万盛区| 黄梅县| 中牟县| 二连浩特市| 合作市| 乡城县| 揭阳市| 石嘴山市| 清流县| 噶尔县| 古浪县| 桦甸市| 全椒县| 周宁县| 安多县| 萨迦县| 孝昌县| 白山市| 安龙县| 汉川市| 富裕县| 洪雅县| 镇江市| 泾川县| 临沭县| 陈巴尔虎旗| 太保市| 晋江市| 本溪市| 开封市| 武强县| 新泰市| 启东市| 仙游县| 营口市| 同江市| 凤台县| 鲁山县| 闸北区|