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?

Kafi
  • 172
  • 1
  • 1
  • 8
  • Possible duplicate of [Special characters in property name of object](https://stackoverflow.com/questions/10455775/special-characters-in-property-name-of-object) – Nigel Ren Jul 05 '18 at 10:38
  • Have you tried without the dot in `htm.com`? – Tvde1 Jul 05 '18 at 10:44

2 Answers2

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'];
Mihai Matei
  • 24,166
  • 5
  • 32
  • 50
1

Try this-

<?php
    $data = '{"hrm.com": { "a": "1",  "b": "c"  }}';
    $character = json_decode($data,true);
    echo $character['hrm.com']['a'];
?>
s.k.paul
  • 7,099
  • 28
  • 93
  • 168