不成功的解析

时间:2013-10-22 11:23:48

标签: iphone ios xcode

以下解析器返回Null值。 任何人都可以帮助我..

-(IBAction)search:(id)sender
{
    NSString *webserviceURL = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/textsearch/json?query=Delhi&sensor=true&key=AIzaSyCGeIN7gCxU8baq3e5eL0DU3_JHeWyKzic"];
    NSURL *finalURl = [NSURL URLWithString:webserviceURL];
    NSData *data = [NSData dataWithContentsOfURL:finalURl];
    NSString *data1 = [NSString stringWithUTF8String:[data bytes]];
    NSLog(@"Result is :%@", data1);
    NSError * error;
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:& error];

    NSLog(@"Result is :%@", json);
}

1 个答案:

答案 0 :(得分:3)

试试这段代码。

NSURL * url=[NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/textsearch/json?query=Delhi&sensor=true&key=AIzaSyCGeIN7gCxU8baq3e5eL0DU3_JHeWyKzic"];

NSData * data=[NSData dataWithContentsOfURL:url];

NSError * error;
NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:& error];

NSLog(@"Result is :%@", json);


NSArray * responseArr = json[@"results"];

for(NSDictionary * dict in responseArr)
{
        [firstArray addObject:[dict valueForKey:@"firstkey"]];
        [secondArray addObject:[dict valueForKey:@"secondkey"]];    
}

    NSLog(@"%@",firstArray);
    NSLog(@"%@",secondArray);

并在数组中获取数据后,执行您想要的操作

按原样使用此代码,并将thius代码替换为您的代码,这将获得您的json响应。