[英]Converting Single Json Object to Json Array in PHP
我正在調用 coincap.io 單個 JSON 對象
{"altCap":282255454377.94916,"bitnodesCount":11508,"btcCap":149160858393,"btcPrice":8830.18849156212,"dom":63.86,"totalCap":431416312770.94904,"volumeAlt":709387849.4057536,"volumeBtc":1253729369.908587,"volumeTotal":1963117219.314342}
我想像數組一樣
[{"altCap":282255454377.94916,"bitnodesCount":11508,"btcCap":149160858393,"btcPrice":8830.18849156212,"dom":63.86,"totalCap":431416312770.94904,"volumeAlt":709387849.4057536,"volumeBtc":1253729369.908587,"volumeTotal":1963117219.314342}]
這是我的主要 php 類
Class Second {
private function request($url)
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
CURLOPT_USERAGENT => 'Agent'
));
return curl_exec($curl);
curl_close($curl);
}
public function jsonCache($ctime)
{
global $request_type, $purge_cache, $limit_reached, $request_limit;
$cache_file = dirname(__FILE__) . '/data/global.json';
$expires = time() - $ctime;
if (!file_exists($cache_file))
die("Cache file is missing: $cache_file");
if (filectime($cache_file) < $expires || file_get_contents($cache_file) == '' || $purge_cache && intval($_SESSION['views']) <= $request_limit) {
$query = 'https://coincap.io/global';
$api_results = $this->request($query);
$json_results = $api_results;
if ($api_results && $json_results)
file_put_contents($cache_file, $json_results);
} else {
$json_results = file_get_contents($cache_file);
$request_type = 'JSON';
}
return $json_results;
}
}
?>
知道如何將它放在方括號中嗎? 已經為數組使用了相同的 PHP 類並且工作正常,但我希望這個單個對象具有用 JSON 文件編寫的方括號
在您的 return 語句中,只需連接括號
return '['.$json_result.']';
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.