如何在我的html代码中打印json属性值?

时间:2017-02-02 07:07:48

标签: php json api

我有一个带有json输出格式here的php文件,我想在另一个php脚本中打印属性值。 在这种情况下,我想打印"描述"属性值就像:

我是样本说明

谢谢

2 个答案:

答案 0 :(得分:0)

谢谢@Deep 通过json_decode它可以工作但不完全。 我的代码在这里。

$url = 'json url here';
$obj = json_decode(file_get_contents($url), true);
echo $obj['result'];

但它可以获得主要属性值。不像“description”这样的子属性,在这种情况下是“products”的子属性。

{
    "result":"success",
    "totalresults":2,
    "products":{
        "product":[{
            "pid":"1",
            "gid":"1",
            "type":"hostingaccount",
            "name":"myproducts",
            "description":"I am sample description",
            "module":"directadmin",
            "paytype":"recurring",
        }]
    }
}

答案 1 :(得分:0)

这是一个完整的解决方案:

$url = 'json url here';
$obj = json_decode(file_get_contents($url), true);
$array = [];
foreach($obj['products'] as $value){
    $array[$value[0]['pid']] = $value[0];
}
echo $array[2]['description'];