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 请问各位,如何在Jetson nano主控板的Ubuntu系统中安装PyQt5
  • ¥15 MAC安装佳能LBP2900驱动的网盘提取码
  • ¥400 微信停车小程序谁懂的来
  • ¥15 ATAC测序到底用什么peak文件做Diffbind差异分析
  • ¥15 安装ubantu过程中第一个vfat 文件挂载失败
  • ¥20 GZ::CTF如何兼容一些靶机?
  • ¥15 etcd集群部署问题
  • ¥20 谁可以帮我一下问一下各位
  • ¥15 为何重叠加权后love图的SMD与svyCreateTableOne函数绘制基线表的不一致
  • ¥150 求 《小魔指》街机游戏机整合模拟软件