0

I'm trying to decode API to PHP.

API EXM

{
"users":[
          {"user":"john",
          "user1":"Tabby",
          "user2":"Ruby",
          "user3":"Silver"}
]
}

tilte users = 0 (It's empty)

I tried with this code but it's not working (blank page)

<?php
$json = '{ "users"[ {"user":"john", "user1":"Tabby", "user2":"Ruby", "user3":"Silver"}';

$arr = json_decode($json);

echo $arr->users->{0}->user; //blank page
echo $arr->users->user1; //blank page
echo $arr->users->0->user2; //syntax error, unexpected '0' (T_LNUMBER), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in
?>
5
  • Your json in the program in invalid. Your missing ]} $arr is null. Commented Feb 2, 2021 at 21:03
  • And after fixing that, consult this question. Commented Feb 2, 2021 at 21:09
  • I believe because the website working with java ("oracle" mentioned in URL). this code just a little example for the real code (full page).. Commented Feb 2, 2021 at 21:10
  • 2
    Try using the [0] syntax to get to that user $arr->users[0]->user Commented Feb 2, 2021 at 21:17
  • Yes users[0] Worked Thanks Commented Feb 2, 2021 at 21:19

2 Answers 2

1

$arr->users is an array and not an object. So if you change your script to

<?php
$json = '{
"users":[
    {"user":"john",
    "user1":"Tabby",
    "user2":"Ruby",
    "user3":"Silver"}
]
}';

$arr = json_decode($json);
print_r($arr);

echo $arr->users[0]->user,"\n";
echo $arr->users[0]->user1,"\n";
echo $arr->users[0]->user2,"\n";
?>

... the output is ...

stdClass Object
(
    [users] => Array
        (
            [0] => stdClass Object
                (
                    [user] => john
                    [user1] => Tabby
                    [user2] => Ruby
                    [user3] => Silver
                )

        )

)
john
Tabby
Ruby
Sign up to request clarification or add additional context in comments.

4 Comments

What if looks like this ? {"address":[{ "AL": "Alabama", "AK": "Alaska", "AS": "American Samoa", "AZ": "Arizona", "AR": "Arkansas", "details":{ "1":{ "total":{ "id1":"140.0", "id2":"140.0" }}} }]} PHP Fatal error: Cannot use object of type stdClass as array
@stackr: My PHP decodes your json sting very well (without error).
yes it's working with first example but its not working with this one ( more levels), {"address":[{ "AL": "Alabama", "AK": "Alaska", "AS": "American Samoa", "AZ": "Arizona", "AR": "Arkansas", "details":{ "1":{ "total":{ "id1":"140.0", "id2":"140.0" }}}}]}
I have no issues with the second string. I did: $j = ' ... '; $a = json_decode($j); print_r($a);
0
<?php
  $json = '{
  "users":[
      {"user":"john",
      "user1":"Tabby",
      "user2":"Ruby",
      "user3":"Silver"}
   ]
 }';
$json = str_replace(array('[',']'),'',$json);
$arr = json_decode($json,true);
print_r($arr);
?>

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.