使用PHP从远程JSON文件中检索数据(单个字段)

时间:2015-06-11 10:29:32

标签: php json

我需要从JSON文件中提取一个字段。从其他问题我已经设法来到这个代码,但输出是一个空白页面。你能看出这个n00b做错了吗?

            <?php
            $q = "https://s3.amazonaws.com/dolartoday/data.json"; 
            $json = file_get_contents($q);
            $details = json_decode($json);
            $tasa=$details->USD[0]->transferencia;
            echo .$tasa.;
        ?>

json文件如下所示:

{
"_antibloqueo": {
    "mobile": "https://dkqrwi4z9077n.cloudfront.net",
    "video": "https://www.youtube.com/embed/videoseries?list=PL6qOmJKmpQ8QMt20uG_dlh9-jzjcvZOxU&showinfo=0",
    "corto_alternativo": "https://bit.ly/venezuela911",
    "enable_iads": "1",
    "alternativo": "68747470733a2f2f643271737267626d6d76366561752e636c6f756466726f6e742e6e6574",
    "alternativo2": "68747470733a2f2f73686431613934666462346474697662672e776f726473736c2e6e6574",
    "resource_id": "33504 C"
},
"_labels": {
    "a": "DOLARTODAY",
    "b": "IMPLICITO",
    "c": "SIMADI",
    "d": "SICAD 2",
    "e": "CENCOEX"
},
"_timestamp": {
    "epoch": "1434016329",
    "fecha": "Junio 11, 2015 05:22 AM",
    "fecha_corta": "Jun 11, 2015",
    "fecha_nice": "Junio 11, 2015",
    "dia": "Jueves",
    "dia_corta": "Jue"
},
"USD": {
    "transferencia": 414.18,
    "transfer_cucuta": 435.00,
    "efectivo": 145.72,
    "efectivo_real": 357.61,
    "promedio": 414.18,
    "promedio_real": 195.94,
    "cencoex": 6.30,
    "sicad1": 12.00,
    "sicad2": 195.94,
    "dolartoday": 414.18
},
"EUR": {
    "transferencia": 466.57,
    "transfer_cucuta": 489.98,
    "efectivo": 164.14,
    "efectivo_real": 402.84,
    "promedio": 466.57,
    "promedio_real": 220.71,
    "cencoex": 7.10,
    "sicad1": 13.52,
    "sicad2": 220.71,
    "dolartoday": 466.57
},
"COL": {
    "compra": 6.70,
    "venta": 7
},
"GOLD": {
    "rate": 1180.0500
},
"USDVEF": {
    "rate": 6.3500
},
"USDCOL": {
    "rate": 2775.00,
    "ratecash": 2396.00,
    "ratetrm": 2523.00,
    "trmfactor": 0.1,
    "trmfactorcash": 0.05
},
"EURUSD": {
    "rate": 1.1264
},
"BCV": {
    "fecha": "1433824200",
    "fecha_nice": "Junio 9, 2015",
    "liquidez": "2.457.797.346",
    "reservas": "16.867.000"
}

}

3 个答案:

答案 0 :(得分:1)

在回复$tasa

时,你只会在USD [0]和2个点错误
$q = "https://s3.amazonaws.com/dolartoday/data.json"; 
$json = file_get_contents($q);
$details = json_decode($json);
$tasa=$details->USD->transferencia;
echo $tasa;

答案 1 :(得分:1)

我测试了你的json,你可以通过下面的多维数组访问数据:

<?php
$q = "https://s3.amazonaws.com/dolartoday/data.json"; 
$json = file_get_contents($q);
$details = json_decode($json , true);

print_r( $details['USD']['transferencia']   );
?>

答案 2 :(得分:1)

试试这个:

    $q = "https://s3.amazonaws.com/dolartoday/data.json"; 
    $json = file_get_contents($q);

    $details = json_decode($json, TRUE);
    var_dump($details['USD']['transferencia']);