-2

以下解析器返回 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);
}
4

1 回答 1

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 响应。

于 2013-10-22T11:52:24.510 回答