从iPhone上的Google API解析JSON [英] Parsing JSON from Google API on iPhone

查看:82
本文介绍了从iPhone上的Google API解析JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已编辑代码

我是这样做的:

// Create new SBJSON parser object
    SBJsonParser *parser = [[SBJsonParser alloc] init];

    // Prepare URL request to download statuses from Twitter
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com/maps?q=restaurant&sll=23.00,72.00&radius=2000&output=json"]];

    // Perform request and get JSON back as a NSData object
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

    // Get JSON as a NSString from NSData response
    NSString *json_string = [[NSString alloc] initWithData:response encoding:NSASCIIStringEncoding];
    [json_string stringByReplacingOccurrencesOfString:@"while(1);" withString:@""];

    NSDictionary *json_dict = [json_string JSONValue];
    NSLog(@"%@",json_dict);

但是我也在json_dict

我想解析从Google Webservice获得的数据.

I want to parse the data which I get from Google Webservice.

http://www .google.com/maps?q = restaurant& sll = 23.00,72.00& radius = 2000& output = json

我使用了以下代码:

// Create new SBJSON parser object
SBJsonParser *parser = [[SBJsonParser alloc] init];

// Prepare URL request for Getting the Restaurent at particular coordinate.
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com/maps?q=restaurant&sll=23.00,72.00&radius=2000&output=json"]];

// Perform request and get JSON back as a NSData object
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

// Get JSON as a NSString from NSData response
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];

// parse the JSON response into an object
// Here we're using NSArray since we're parsing an array of JSON status objects
NSArray *statuses = [parser objectWithString:json_string error:nil];

但是我没有得到答复.如果我使用其他类似的API,例如: http://twitter.com/statuses/public_timeline.json ,它将正确解析,但这不适用于Google API

But I am not getting the response. If I use some other API like: http://twitter.com/statuses/public_timeline.json, it will parse properly, but this does not work for Google APIs

我已经检查了JSON版本的响应字符串.我们得到的响应正在形成一棵正确的树.它没有显示任何错误,但是响应中的数组为空.

I had checked the response string in JSON version. The response which we get is forming a proper tree. It is not showing any error but the array in response which I get is null.

推荐答案

编辑:

我正在调查并发现,这不是正确的响应,因为您获取的文件带有JSON扩展名,而不是JSON响应(与您引用的twitter api一样).因此,我的建议是参考 google place api 并找到以下列表的JSON响应:餐馆,然后重试.

I was looking into and found out that it is not a proper response as you get a file with JSON extension and not a JSON response as is the case with twitter api you are referring. So my advice refer google places api and find a JSON response for a list of restaurants and then try again.

要点击此处的api密钥,您需要使用您自己的api密钥进行身份验证,您可以使用

In order to hit there api key you need authenticate with your own api key which you get using this.

一旦您获得了api密钥,就可以像这样敲击api.

once you get your api key you can hit the api like this..

作为回报,这将为您提供一个JSON响应,您可以轻松地对其进行解析(这将返回一个字典),完成您的操作(此api每天仅限100次点击.)...希望这会有所帮助.

this in return will give you a JSON response which you can parse easily (this returns a dictionary) and voila you are done(this api is limited to 100 hits per day only.)... hoping this helps.

因此您的代码将如下所示,并且在我检查过之后可以正常工作...

So your code will look like this and will work I have checked it...

/// Create new SBJSON parser object 
    SBJsonParser *parser = [[SBJsonParser alloc] init];

    // Prepare URL request to download statuses from Twitter
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=5000&types=food&sensor=false&key=yourOwnAPIKey"]];

    // Perform request and get JSON back as a NSData object
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

    // Get JSON as a NSString from NSData response
    NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];

    // parse the JSON response into an object
    // Here we're using NSArray since we're parsing an array of JSON status objects
    NSArray *statuses = [parser objectWithString:json_string error:nil];

    NSLog(@"statuses:%@", statuses);  

这篇关于从iPhone上的Google API解析JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆