我想从JSON的几个字符串中删除",该字符串每隔几分钟更新一次(http://zergpool.com/api/status(。
例如:
{"bitcore":{"name":"bitcore","port":3556,"coins":1,"fees":0,"hashrate":0,"workers":0,"estimate_current":"0.00001745","estimate_last24h":"0.00001756","actual_last24h":"0.00000","hashrate_last24h":105820474.1458},
字段:
"estimate_current":"0.00001745" -> "estimate_current":0.00001745
"estimate_last24h":"0.00001756" -> "estimate_last24h":0.00001756
"actual_last24h":"0.00000" -> "actual_last24h":0.00000
由于数字一直在变化,因此是否可以编写PHP实时转换它们?这就是我所做的。
<?php
$url = 'http://zergpool.com/api/status';
$data = file_get_contents($url);
$manage = json_decode($data,true);
//$aha = (int)preg_replace("/[^d]+/","",$manage); // tried removing them like this... doesn't work.
echo json_encode($manage)
不起作用:(
您可以使用它从JSON中的数字值中删除引号。
$encoded = json_encode($data, JSON_NUMERIC_CHECK);
版本中支持>= PHP 5.3
echo str_replace( '"', '' ,$data);
将删除所有双引号。
我不明白您为什么要从$管理行中删除双引号。您只需访问以$管理返回并转换为float的JSON的元素。
$firstString = $manage['bitcore']['estimate_current'];
$firstFloat = (float)$firstString;
var_dump($firstFloat);
或
echo 'floatval=' . floatval($firstString);
尝试以下:
$json = file_get_contents("https://www.ahashpool.com/api/status/");
$ar = json_decode($json, TRUE);
$filter = [];
foreach ($ar as $k => $sub_ar) {
foreach ($sub_ar as $sub_k => $sub_v) {
if(preg_match('/^[0-9]*.[0-9]+$/', $sub_v)){
$filter[$k][$sub_k] = (float) $sub_v;
} else {
$filter[$k][$sub_k] = $sub_v;
}
}
}
echo "<pre>";
var_dump($filter);
die();