0

I am trying to decode JSON string as follows-

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

I am getting the error

Undefined property: stdClass::$hrm

Any help?

2

2 Answers 2

3

One single json_decode is enough:

$character = json_decode($data);

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

Or you can use the second parameter of the json_decode function to return an associative array:

$character = json_decode($data, true);

echo $character['hrm.com']['a'];
Sign up to request clarification or add additional context in comments.

Comments

1

Try this-

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.