【问题标题】:Parsing JSON from Google API on iPhone从 iPhone 上的 Google API 解析 JSON
【发布时间】:2012-03-05 10:20:50
【问题描述】:

编辑代码

我是这样做的:

// 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中得到null

我想解析从 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

我检查了 JSON 版本的响应字符串。我们得到的响应是形成一棵合适的树。它没有显示任何错误,但我得到的响应数组为空。

【问题讨论】:

    标签: iphone json


    【解决方案1】:

    编辑

    我正在调查并发现它不是正确的响应,因为您获得的是带有 JSON 扩展名的文件,而不是您所指的 twitter api 的 JSON 响应。所以我的建议是参考google places api 并找到一个餐厅列表的 JSON 响应,然后再试一次。

    为了获得 api 密钥,您需要使用自己的 api 密钥进行身份验证,您使用 this 获得该密钥。

    一旦你得到你的 api 密钥,你就可以像这样点击 api..

    https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&types=food&name=harbour&sensor=false&key=AddYourOwnKeyHere

    这反过来会给你一个 JSON 响应,你可以轻松解析它(这会返回一个字典),瞧你就完成了(这个 api 仅限于每天 100 次点击。)...希望这会有所帮助。

    所以你的代码看起来像这样,我已经检查过了......

    /// 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);  
    

    【讨论】:

    • 感谢您的快速回复。我什至尝试过使用字典,但我得到的回复总是空的..
    • @Thanks Ankit..for the reply..我已经创建了谷歌密钥并试图获取数据..但我得到的响应是 REQUEST_DENEID..我使用的 url 是:- maps.googleapis.com/maps/api/place/search/…
    • 她说它适用于她在 google api 之后提到的 twitter api(从下面阅读第 6 行,然后重新阅读我的答案。)
    • 你确定你在身份验证中为place api启用了它...?因为这就是我所做的并且对我有用。
    • @shweta 嘿,你的钥匙现在可以用了。试试这个它包含你的钥匙。maps.googleapis.com/maps/api/place/search/…
    【解决方案2】:

    使用这个

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

    然后你可以使用它的键获取任何值

    【讨论】:

    • @感谢您的回复,我已经这样做了..但仍然没有得到回复。已经编辑了问题
    【解决方案3】:

    来自http://www.google.com/maps?q=restaurant&sll=23.00,72.00&radius=2000&output=json 的响应字符串以while(1); 开头,它不是有效的 JSON,删除它应该可以工作。

    【讨论】:

    • @感谢您的回复..我已经这样做并编辑了我的问题..请检查并帮助我..
    【解决方案4】:

    您可以通过将 url 替换为此来使用 google places api

    https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=2000&types=restaurant&name=harbour&sensor=false&key=AddYourOwnKeyHere

    从 Google“https://code.google.com/apis/console/?pli=1#project:305239127220”生成您的密钥

    【讨论】:

    • 使用了这个但是得到了这个错误:-{ "html_attributions" : [], "results" : [], "status" : "REQUEST_DENIED" }
    • 你用你生成的密钥替换了密钥,还尝试在简单的网络浏览器上运行 url 并检查它返回的内容。
    • 我已经创建了 apikey,甚至更改了传感器参数。在这个 url-code.google.com/apis/maps/documentation/places 中进行了解释,但随后也被拒绝访问
    • 您是否从 Google 控制台启用了 Google 地方服务“code.google.com/apis/console/…”从这个链接检查
    • 这是您的密钥错误,无法从 Google 服务器获得访问权限,我尝试了相同的链接,将您的密钥更改为我的密钥,并且工作正常。尝试删除此密钥并生成您的密钥。希望这会有所帮助
    【解决方案5】:

    您可以直接将 URL 中的内容下载到您的 jsonString 中,而不是创建 URLRequest,就像这样

    NSString * jsonString = [[NSString alloc] initWithContentsOfURL:@"http://www.google.com/maps?q=restaurant&sll=23.00,72.00&radius=2000&output=json" encoding:NSUTF8StringEncoding error:nil];
    

    并使用 JSONParser 将此 jsonString 转换为 NSDictionary。

    希望这能解决您的问题.....

    【讨论】:

      【解决方案6】:

      试试这个

      使用SBJson 代替jsonValue 方法。

      1) 导入"SBJSON.h"in .h ViewController。

      NSString *jsonString;
      //jsonString this is The Coming JSON Response.
      
      SBJSON *parser = [[[SBJSON alloc] init] autorelease];
      NSDictionary *jsonResponse = (NSDictionary*)[parser objectWithString:jsonString error:nil];
      NSDictionary *responseData = [jsonResponse objectForKey:@"response"]; objectForKey:@"venues"] ;
      //Further parsed the Data According to the Keys.
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-14
        • 2017-12-18
        相关资源
        最近更新 更多