来自 Google Finance API 调用的 JSON 中的前导斜杠

发布于 2024-10-12 20:14:14 字数 1211 浏览 5 评论 0原文

我一直在使用 Google Finance API 成功收集一些股票信息。问题是,在调用 http://www.google.com/finance/info?infotype=infoquoteall&q=[$tickerSymbol] 后,Google 返回的 JSON 具有 / / 添加在其前面,因此无法使用 PHP 的 json_encode() 对字符串进行编码。 JSONLint JSON 验证器 确认 // 无效。明显的解决方法是去掉 JSON 开头的斜杠。尽管如此,我还是想知道为什么 Google 在返回的 JSON 中添加斜杠。额外的斜杠背后有什么目的吗?当其他语言简单地忽略额外字符时,这是 PHP 的 json_encode() 的一个怪癖吗?我做错了什么吗?

以下是对带有前导斜杠的 http://www.google.com/finance/info?infotype=infoquoteall&q=AAPL 请求的结果示例。

// [ {
"id": "22144"
,"t" : "AAPL"
,"e" : "NASDAQ"
,"l" : "340.65"
,"l_cur" : "340.65"
,"ltt":"4:00PM EST"
,"lt" : "Jan 18, 4:00PM EST"
,"c" : "-7.83"
,"cp" : "-2.25"
,"ccol" : "chr"
,"el": "345.20"
,"el_cur": "345.20"
,"elt" : "Jan 18, 5:45PM EST"
,"ec" : "+4.55"
,"ecp" : "1.34"
,"eccol" : "chg"
,"div" : ""
,"yld" : ""
,"eo" : ""
,"delay": ""
,"op" : "327.05"
,"hi" : "344.76"
,"lo" : "326.00"
,"vo" : "66.34M"
,"avvo" : "11.28M"
,"hi52" : "348.48"
,"lo52" : "190.25"
,"mc" : "313.75B"
,"pe" : "22.49"
,"fwpe" : ""
,"beta" : "1.38"
,"eps" : "15.15"
,"name" : "Apple Inc."
,"type" : "Company"
}
]

I've been using the Google Finance API to successfully gather some stock info. The problem is that after a call to http://www.google.com/finance/info?infotype=infoquoteall&q=[$tickerSymbol], the JSON that Google returns has // added before it and therefore the string cannot be encoded using PHP's json_encode(). The JSONLint JSON Validator confirms that the //s are not valid. The obvious workaround is to strip the slashes from the beginning of the JSON. None-the-less, I am left wondering why Google is adding slashes to the JSON it is returning. Is there a purpose behind the extra slashes? Is this a quirk with PHP's json_encode() when other languages would simply ignore the extra characters? Am I doing something incorrectly?

Here is an example of the result of a request for http://www.google.com/finance/info?infotype=infoquoteall&q=AAPL with the leading slashes.

// [ {
"id": "22144"
,"t" : "AAPL"
,"e" : "NASDAQ"
,"l" : "340.65"
,"l_cur" : "340.65"
,"ltt":"4:00PM EST"
,"lt" : "Jan 18, 4:00PM EST"
,"c" : "-7.83"
,"cp" : "-2.25"
,"ccol" : "chr"
,"el": "345.20"
,"el_cur": "345.20"
,"elt" : "Jan 18, 5:45PM EST"
,"ec" : "+4.55"
,"ecp" : "1.34"
,"eccol" : "chg"
,"div" : ""
,"yld" : ""
,"eo" : ""
,"delay": ""
,"op" : "327.05"
,"hi" : "344.76"
,"lo" : "326.00"
,"vo" : "66.34M"
,"avvo" : "11.28M"
,"hi52" : "348.48"
,"lo52" : "190.25"
,"mc" : "313.75B"
,"pe" : "22.49"
,"fwpe" : ""
,"beta" : "1.38"
,"eps" : "15.15"
,"name" : "Apple Inc."
,"type" : "Company"
}
]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别再吹冷风 2024-10-19 20:14:14

对于那些正在寻找现成答案的人,这里有一个 PHP 的工作示例; JSON 被清理并转换为对象。可以轻松提取值。

第二个只是为了让它更棒,当页面被访问时,它会向你的 PubNub 频道发送一条推送消息(cron 是你的朋友)。 PubNub 消息可以通过 javascript 轻松接收,因此是实时的……

<?php

  //Obtain Quote Info
  $quote = file_get_contents('http://finance.google.com/finance/info?client=ig&q=INDEXDB:DAX');

  //Remove CR's from ouput - make it one line
    $json = str_replace("\n", "", $quote);

  //Remove //, [ and ] to build qualified string  
    $data = substr($json, 4, strlen($json) -5);

  //decode JSON data
    $json_output = json_decode(utf8_decode($data));

  // get the last price
    $last = $json_output->l;

  //Output Stock price .
  echo 'DAX: ' . $last; 


//////////////////////////////
//  send it through pubnub  //
//////////////////////////////

require_once('Pubnub.php');

// Publish and Subscribe Keys 
$publish_key   = 'demo';
$subscribe_key = 'demo';
$subscribe_key = false;

// Create Pubnub Object
$pubnub = new Pubnub( $publish_key, $subscribe_key, $secret_key );

// Publish !
$channel = 'quoteTheDax';

$timestamp = $pubnub->time();
$pubish_success = $pubnub->publish(array(
    'channel' => $channel,
    'message' => array("last" => $last2, "ts" => $timestamp)
));

//Boom its send to ppl subscribed to this channel arround the world
?>

当然,如果您需要一直实时更新,还有更好的选择。我只是想每 30 分钟/60 分钟更新一次。

For those looking for a ready answer, here is a working example with PHP; The JSON is cleaned and transformed into an object. Values can easily be extracted.

The second is just to make it more awesome, it sends a push message you a PubNub channel when page is accessed (cron is your friend). PubNub message can easily be received via javascript hence live...

<?php

  //Obtain Quote Info
  $quote = file_get_contents('http://finance.google.com/finance/info?client=ig&q=INDEXDB:DAX');

  //Remove CR's from ouput - make it one line
    $json = str_replace("\n", "", $quote);

  //Remove //, [ and ] to build qualified string  
    $data = substr($json, 4, strlen($json) -5);

  //decode JSON data
    $json_output = json_decode(utf8_decode($data));

  // get the last price
    $last = $json_output->l;

  //Output Stock price .
  echo 'DAX: ' . $last; 


//////////////////////////////
//  send it through pubnub  //
//////////////////////////////

require_once('Pubnub.php');

// Publish and Subscribe Keys 
$publish_key   = 'demo';
$subscribe_key = 'demo';
$subscribe_key = false;

// Create Pubnub Object
$pubnub = new Pubnub( $publish_key, $subscribe_key, $secret_key );

// Publish !
$channel = 'quoteTheDax';

$timestamp = $pubnub->time();
$pubish_success = $pubnub->publish(array(
    'channel' => $channel,
    'message' => array("last" => $last2, "ts" => $timestamp)
));

//Boom its send to ppl subscribed to this channel arround the world
?>

of course if you need something live updating all the time there are better options. I was just looking to update every 30min/60min.

淡看悲欢离合 2024-10-19 20:14:14

我猜这是因为 google 不希望您使用该 JSON,他们建议使用 Google Data API。

I guess it's because google don't want you to work with that JSON, they recommend to use the Google Data API.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文