ÔÚ֮ǰµÄ¡¶iOSѧϰ——xmlÊý¾Ý½âÎö£¨¾Å£©¡· ½éÉÜÁËxmlÊý¾Ý½âÎö£¬Õâһƪ¼òµ¥½éÉÜÒ»ÏÂJsonÊý¾Ý½âÎö¡£JSON ¼´ JavaScript Object Natation£¬ËüÊÇÒ»ÖÖÇáÁ¿¼¶µÄÊý¾Ý½»»»¸ñʽ£¬·Ç³£ÊʺÏÓÚ·þÎñÆ÷Óë¿Í»§¶ËµÄ½»»¥£¬JsonÓï·¨²Î¿¼¡£¹ØÓÚÔÚiOSƽ̨ÉϽøÐÐJSON½âÎö£¬ÒѾÓкܶàµÚÈý·½µÄ¿ªÔ´ÏîÄ¿£¬±ÈÈçTouchJson£¬JSONKit£¬SBJonµÈ£¬×Ô´ÓiOS5.0ÒÔºó£¬Æ»¹ûSDKÍƳöÁË×Ô´øµÄJSON½â¾ö·½°¸NSJSONSerialization£¬ÕâÊÇÒ»¸ö·Ç³£ºÃÓõÄJSONÉú³ÉºÍ½âÎö¹¤¾ß£¬Ð§ÂÊÒ²ÊDZÈÆäËûµÚÈý·½¿ªÔ´ÏîÄ¿µÄ¸ßºÜ¶à£¬ÏêÇé¿É¿´ÏÂͼ¡£
ͼƬÏêÇé¿É²é¿´
NSJSONSerializationÌṩÁËJsonÊý¾Ý·â°ü¡¢JsonÊý¾Ý½âÎö£¬NSJSONSerialization½«JSONÊý¾Ýת»»ÎªNSDictionary»òNSArray½â°ü·½·¨£¬½«NSDictionary¡¢NSArray¶ÔÏóת»»ÎªJSONÊý¾Ý£¨¿ÉÒÔͨ¹ýµ÷ÓÃisValidJSONObjectÀ´ÅжÏNSDictionary¡¢NSArray¶ÔÏóÊÇ·ñ¿ÉÒÔת»»ÎªJSONÊý ¾Ý£©·â°ü¡£Õâһƪ½«×ö¼òµ¥½éÉÜ¡£
JsonÊý¾Ý·â°ü
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"value1",@"key1",@"value2",@"key2",@"value3",@"key3", nil];
// isValidJSONObjectÅж϶ÔÏóÊÇ·ñ¿ÉÒÔ¹¹½¨³Éjson¶ÔÏó
if ([NSJSONSerialization isValidJSONObject:dic]){
NSError *error;
// ´´ÔìÒ»¸öjson´ÓData, NSJSONWritingPrettyPrintedÖ¸¶¨µÄJSONÊý¾Ý²úµÄ¿Õ°×£¬Ê¹Êä³ö¸ü¾ß¿É¶ÁÐÔ¡£
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error];
NSString *json =[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"json data:%@",json);
}
JsonÊý¾Ý½âÎö
NSError *error;
//¼ÓÔØÒ»¸öNSURL¶ÔÏó
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.weather.com.cn/data/101120101.html"]];
//½«ÇëÇóµÄurlÊý¾Ý·Åµ½NSData¶ÔÏóÖÐ
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//IOS5×Ô´ø½âÎöÀàNSJSONSerialization´ÓresponseÖнâÎö³öÊý¾Ý·Åµ½×ÖµäÖÐ
NSDictionary *weatherDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
NSDictionary *weatherInfo = [weatherDic objectForKey:@"weatherinfo"];
NSString *text = [NSString stringWithFormat:@"½ñÌìÊÇ %@ %@ %@ µÄÌìÆø×´¿öÊÇ£º%@ %@ ",[weatherInfo objectForKey:@"date_y"],[weatherInfo objectForKey:@"week"],[weatherInfo objectForKey:@"city"], [weatherInfo objectForKey:@"weather1"], [weatherInfo objectForKey:@"temp1"]];
NSLog(@"weatherInfo£º%@", text );
¡¡