Php 从JSON中删除不需要的值
我有下面的文本,我想剥离出来并获取数据Php 从JSON中删除不需要的值,php,json,Php,Json,我有下面的文本,我想剥离出来并获取数据 (function() {})({ "Data": { "Status": "SUCCESS", "Name": "Facebook Inc", "Symbol": "FB", "LastPrice": 31.91, "Change": -1.12, "ChangePercent": -3.39085679685135, "Timesta
(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
}
})
我还有下面的代码正在使用GoogleAPI,它将在今年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 Finance的代码是直接定制的,用于处理您使用的Google Finance API的输出
以下是Google Finance的输出:
// [
{
"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'];
您试图删除哪些字符?(function(){})({“Data”:{这是一个Google API请求的结果,还是你从某个地方刮来的?你能发布顶部数组的url吗…那是从其他地方来的…我不想再使用Google finance API了,因为它正在消失。我想去掉输出中不需要的字符。我的GF代码也在这样做,但是有了这个新的link。但我一辈子都不能让它工作。我的答案是针对新的输出。最后的四行代码完全符合你的要求。为我工作的伟人。非常感谢。我学习json大约3天了。我掌握了它,但一直坚持这一点。提供jsonpjson填充的API通常提供andard json,99.9%的时候它只是删除了p。你也许还应该使用curl来获取结果,因为它会稍微快一点。是的,我还没有开始使用curl。你有没有一个这样的例子?@DavidMorin我用一个简单的curl函数更新了我的答案。哦,太好了。谢谢你,伙计。我下周将谈到curl.