ios-xml和json的解析

#import "LYAppDelegate.h"
#import  <Foundation/Foundation.h>
#import "XMLReader.h"
#import "JSONKit.h"

@implementation LYAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
  //  NSURL *url=[NSURL URLWithString:@"http://www.w3school.com.cn/example/xmle/note.xml"];//xml
    NSURL *urljson=[NSURL URLWithString:@"http://maps.googleapis.com/maps/api/geocode/json?latlng=39.904299,116.22169&sensor=true"];//json
    NSMutableURLRequest* request= [[NSMutableURLRequest alloc]initWithURL:urljson cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
    NSOperationQueue*queue=  [[NSOperationQueue alloc]init];
//    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//        NSError * error=nil;
//        NSDictionary *dic=[XMLReader dictionaryForXMLData:data error:&error];
//        NSLog(@"%@",[[[dic objectForKey:@"note"] objectForKey:@"from"] objectForKey:@"text"]);
//        
//    }];
    
    
    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSDictionary * dic=[data objectFromJSONData];
         NSLog(@"dic%@",[[dic objectForKey:@"results"][5]objectForKey:@"formatted_address"]);
    }];//json解析
    return YES;
}
@end
xml的解析我们用XMLReader,json的解析我们用JSONKit,这2库已经同步到百度云了,用的时候去拿,同时JSONkit依赖foundatin框架的libz.dylib所以用的时候必须导入。

 

posted @ 2014-05-06 15:49  离子  阅读(196)  评论(0编辑  收藏  举报