在PHP中解析嵌套的JSON

时间:2018-07-05 10:31:43

标签: php json decode

我正在尝试如下解码JSON字符串-

<?php
    $data = '{"hrm.com": { "a": "1",  "b": "c"  }}';
    $character = json_decode($data);
    $character = json_decode($character->hrm.com);
    echo $character->a;
?>

我遇到了错误

Undefined property: stdClass::$hrm

有帮助吗?

2 个答案:

答案 0 :(得分:3)

一个json_decode就足够了:

$character = json_decode($data);

echo $character->{'hrm.com'}->a;

或者您可以使用json_decode函数的第二个参数来返回关联数组:

$character = json_decode($data, true);

echo $character['hrm.com']['a'];

答案 1 :(得分:1)

尝试一下-

<?php
    $data = '{"hrm.com": { "a": "1",  "b": "c"  }}';
    $character = json_decode($data,true);
    echo $character['hrm.com']['a'];
?>