【问题标题】:JSON to PHP variable with integer [duplicate]JSON到带有整数的PHP变量[重复]
【发布时间】:2017-01-08 16:30:04
【问题描述】:

我有这个 JSON 输出:

{"ok":true,"license":"CC BY 4.0 -  http:\/\/creativecommons.tankerkoenig.de","data":"MTS-K","prices":{"90d20149-1047-43d3-9b80-4c6f7fd22499":{"status":"open","e5":1.479,"e10":1.459,"diesel":1.259}}}

我想将 e5、e10 和柴油的值放入 PHP 变量中。喜欢:

$Tankstelle = json_decode($json);
$PriceE5 = (float)utf8_decode($Tankstelle->prices->90d20149-1047-43d3-9b80-4c6f7fd22499->e5);

但它不起作用,因为有这个整数作为值。你能帮助我吗?谢谢!

【问题讨论】:

    标签: php json


    【解决方案1】:

    如果值 '90d20149-1047-43d3-9b80-4c6f7fd22499' 是可变的,您可以通过以下方式进行:

    $json = '{"ok":true,"license":"CC BY 4.0 -  http:\/\/creativecommons.tankerkoenig.de","data":"MTS-K","prices":{"90d20149-1047-43d3-9b80-4c6f7fd22499":{"status":"open","e5":1.479,"e10":1.459,"diesel":1.259}}}';
    $Tankstelle = json_decode($json, true);
    $item = array_pop($Tankstelle['prices']);
    $PriceE5 = (float)utf8_decode($item['e5']);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-05
      • 1970-01-01
      • 1970-01-01
      • 2020-04-01
      相关资源
      最近更新 更多