从 JSON 中删除不需要的值


Strip unwanted values from JSON

我有以下文本,我想删除并获取数据。

(function() {})({
    "Data": {
        "Status": "SUCCESS",
        "Name": "Facebook Inc",
        "Symbol": "FB",
        "LastPrice": 31.91,
        "Change": -1.12,
        "ChangePercent": -3.39085679685135,
        "Timestamp": "Fri May 25 16:00:05 UTC-04:00 2012",
        "MarketCap": 20214729720,
        "Volume": 37189630,
        "ChangeYTD": 0,
        "ChangePercentYTD": 0,
        "High": 32.95,
        "Low": 31.11,
        "Open": 32.9
    }
})

我还有以下代码,它正在使用谷歌 api,它将在今年 10 月离开我们。

<?php
//Obtain Quote Info
$quote = file_get_contents('http://finance.google.com/finance/info?client=ig&q=NASDAQ:'  . $stock . '');
//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
$perc = $json_output->c;
$last = $json_output->l;
$date = $json_output->lt;
$name = $json_output->t;
?>

无论出于何种原因,我都无法弄清楚如何让对方使用我的代码。 有人有什么建议吗?

只需使用 http://dev.markitondemand.com/Api/Quote/json?symbol=AAPL 作为 url,省略 json 中的 p。

<?php
$stock = "FB";
//Obtain Quote Info
//$quote = file_get_contents('http://dev.markitondemand.com/Api/Quote/json?symbol='.$stock);
//or with curl
$quote = curl_get('http://dev.markitondemand.com/Api/Quote/json?symbol='.$stock);
function curl_get($url){
    if (!function_exists('curl_init')){
        die('Sorry cURL is not installed!');
    }
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_REFERER, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/1.0");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}
//decode JSON data
$json_output = json_decode(utf8_decode($quote));
$perc = $json_output->Data->ChangePercent;
$last = $json_output->Data->LastPrice;
$date = $json_output->Data->Timestamp;
$name = $json_output->Data->Name;
print_r($json_output);
/*
stdClass Object
(
    [Data] => stdClass Object
        (
            [Status] => SUCCESS
            [Name] => Facebook Inc
            [Symbol] => FB
            [LastPrice] => 31.91
            [Change] => -1.12
            [ChangePercent] => -3.3908567968514
            [Timestamp] => Fri May 25 16:00:05 UTC-04:00 2012
            [MarketCap] => 20214729720
            [Volume] => 37189630
            [ChangeYTD] => 0
            [ChangePercentYTD] => 0
            [High] => 32.95
            [Low] => 31.11
            [Open] => 32.9
        )
)*/
?>

您用于 Google 财经的代码是直接自定义的,以与您正在使用的 Google 财经 API 的输出配合使用。

以下是谷歌财经的输出:

// [
{
"id": "296878244325128"
,"t" : "FB"
,"e" : "NASDAQ"
,"l" : "31.91"
,"l_cur" : "31.91"
,"s": "0"
,"ltt":"4:00PM EDT"
,"lt" : "May 25, 4:00PM EDT"
,"c" : "-1.12"
,"cp" : "-3.39"
,"ccol" : "chr"
}
]

代码中执行以下操作的部分:

$data = substr($json, 4, strlen($json) -5);

正好从 GF 输出中去除不需要的字符。对于包装在匿名函数中的新输出,我建议如下:

<?php
preg_match('/'(function'(') '{'}')'((.*)')/', $input_string, $matches);
$data_json = $matches[1];
$json_output = json_decode($data_json, true); //true = assoc array
$final_json = $json_output['Data'];

这会将一个有效的 json 字符串分配给 $json 变量,其中 $input_string 是您在问题中列出的输入。$final_json 将包含 Data 中的任何内容:原始 json 的属性。