doutou7740 2015-03-08 22:44
浏览 89
已采纳

从php问题的URL获取JSON对象

I have the following php:

<?php
$json = file_get_contents('https://www.cryptonator.com/api/full/doge-usd');
$obj = json_decode($json);
echo $obj->ticker.price;
?>

This returns a blank page. Yet when I do:

echo $json; 

I get the full JS:

{"ticker":{"base":"DOGE","target":"USD","price":"0.00013826","volume":"14555353.11203900","change":"-0.00000236","markets":[{"market":"Bleutrade","price":"0.00014400","volume":1.49328868},{"market":"Cex.io","price":"0.00013864","volume":6236157},{"market":"Comkort","price":"0.00014990","volume":412.40655758},{"market":"Cryptsy","price":"0.00013853","volume":382887.92442395},{"market":"Exmo","price":"0.00013800","volume":7932091.2389102},{"market":"Nix-e","price":"0.00011100","volume":539.10150086},{"market":"useCryptos","price":"0.00000305","volume":3263.94735765}]},"timestamp":1425854462,"success":true,"error":""}

I just want to create a variable that contains the value within the 'price' field.

Thanks.

  • 写回答

4条回答 默认 最新

  • dtlhy0771 2015-03-08 22:47
    关注

    The json_encode() call returns a nested object. So you have to address the price attribute like that:

    echo $obj->ticker->price;
    

    The . operator you coded is a string concatenation in php. That is not what you want. php tries to concatenate two strings, the first being an object of type stdClass for which no string conversion routine exists. That is why you get the error.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 springboot国际化
  • ¥15 搭建QEMU环境运行OP-TEE出现错误
  • ¥15 Minifilter文件保护
  • ¥15 有限元软件终止时间超过设定值
  • ¥15 onvif框架引用一直报错
  • ¥50 C#和C++混合编程,使用CLR托管,报错System.Runtime.InteropServices.SEHException
  • ¥30 .NET使用sqlite发布后报错
  • ¥15 Unity在WebGL平台导出Word报错问题
  • ¥15 ghpython这里总是报错而且rhino视图窗口内不显示怎么办
  • ¥15 布尔运算推导,请证明