2012-05-28 39 views
0

我有以下文本,我想去掉並獲取數據。從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的工作,這將在今年十月離開我們。

<?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; 
?> 

無論出於何種原因,我無法弄清楚如何讓其他人使用我的代碼。任何人有任何建議?

+0

你想擺脫哪些角色? –

+0

(函數(){})({ 「數據」:{ –

+0

是否是Google API請求的結果,還是你從某處刮取? –

回答

0

只需使用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 
     ) 

)*/ 
?> 
+0

偉人這對我很有幫助,非常感謝,我在約3天的時間裏學習了json,我抓住了它,但被卡在了這個 –

+0

提供jsonp'JSON with padding'的API通常提供標準的json,99.9%的時間只是它的正義刪除p。你也許應該使用curl來獲得結果,因爲它會稍快。 –

+0

是的,我還沒有跳進cURL呢。你有什麼樣的例子嗎? –

-1

您用於Google財經的代碼是直接自定義的,以便與您使用的Google財經API的輸出結合使用。

下面是來自谷歌財經的輸出:

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

正好剝出從GF輸出不需要的字符:

// [ 
{ 
"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" 
} 
] 

,做的代碼的一部分。爲了您的新的輸出包裹的匿名函數裏面,我建議如下:

<?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將包含原始json的Data:屬性內的任何內容。

+0

我不想再使用谷歌財務API了,因爲它正在消失。我想剝離輸出中不需要的字符。因此,像我的GF代碼一樣,但這種新的鏈接。但我不能爲了我的生活而開始工作。 –

+0

我的答案是針對新的輸出。最後的四行代碼完全符合你的要求。 –