Notice: Trying to get property of non-object in - php

I'm learning a bit of PHP. It's supposed to print 0, however I get an error:
Notice: Trying to get property of non-object in...
<?php
$json = '[{"assetref":"","qty":0,"raw":0}]';
$obj = json_decode($json);
print $obj->{'qty'}; // Result 0
?>

The brackets on the outside of your JSON string are causing it to be an object inside of an array.
You can access the object by specifying which array member you want with $obj[0]->{'qty'};
OR change your json string so it instantiates into an object directly.
$json = '{"assetref":"","qty":0,"raw":0}';

Related

Trying to get multi sub-level JSON using Stripe API [duplicate]

I'm learning a bit of PHP. It's supposed to print 0, however I get an error:
Notice: Trying to get property of non-object in...
<?php
$json = '[{"assetref":"","qty":0,"raw":0}]';
$obj = json_decode($json);
print $obj->{'qty'}; // Result 0
?>
The brackets on the outside of your JSON string are causing it to be an object inside of an array.
You can access the object by specifying which array member you want with $obj[0]->{'qty'};
OR change your json string so it instantiates into an object directly.
$json = '{"assetref":"","qty":0,"raw":0}';

Accessing a JSON value with a php $Key value

While answering Get JSON object from URL Difficulties, I noticed one of the JSON names was "$id":
{ "data" : [
{
"$id": "1",
"SearchKey": "Alnwick |Alnwick",
...
This caused the following php code to throw different errors:
$json = ... //json above
$obj = json_decode($json);
echo property_exists($obj->data[0], '$id'); // prints true
echo $obj->data[0]->$id; // PHP Fatal Error: Cannot access empty property ...
echo $obj->data[0]->id; // PHP Notice: Undefined property stdClass::$id ...
echo $obj->data[0]->'$id'; // PHP Parse Error: syntax error, unexpected ''$id'' (T_CONSTANT_ENCAPSED_STRING) ...
Assuming the json is decoded as objects not arrays, how can I access the "$id" field?
Accessing the variable via {'invalid-parameter-name'} works:
echo $obj->data[0]->{'$id'}; // 1

Accessing Object Properties returned from ASP.net

I'm working on my first from-scratch back end project. Forgive my ignorance.
I have a page that's filled by data returned from an ASP.net API. I'm connecting to the API successfully using SoapClient, but I'm unable to successfully parse the results.
How can I echo Status in the object below?
The returned object is:
stdClass Object(
[LoginResult] => {
"Result":{
"Status":"FAILED",
"Message":"Access Denied"},
"SessionToken":""
}
)
My code is:
$loginResult->Result;
The error I receive is:
Undefined property: stdClass::$Result.
If $loginResult is the variable of the returned result then it is an object with a property LoginResult that contains a JSON encoded object. Once decoded as an array, it has a Result key array containing the keys Status and Message:
$array = json_decode($loginResult->LoginResult, true);
echo $array['Result']['Status'];
If you don't pass true to json_decode then you get a decoded object containing another object and would use:
$object = json_decode($loginResult->LoginResult);
echo $object->Result->Status;
In PHP >= 5.4.0 you should be able to do:
echo json_decode($loginResult->LoginResult, true)['Result']['Status'];
// or
echo json_decode($loginResult->LoginResult)->Result->Status;

Illegal string offset 'properties', how to pass json encoded values

how do I assign a field of a json_encoded result to a variable. I have the following:
$jsonres = json_encode($result); //where result is an array holding fields including name (string), properties (object type or array)
I tried the following:
echo $jsonres['properties']; // failed with "Illegal string offset 'properties'"
var_dump ($jsonres->properties); //"Notice: Trying to get property of non-object in..."
I need to be able to use the value of 'properties' in my form.
Thanks
Simply encode the properties property, not the entire object:
$jsonres = json_encode($result['properties']);
echo $jsonres;

Can't get value from array

I'm trying to output the value of the email value of an array, but have problems doing so.
The array is based on json_decode()
This is the error I receive
Fatal error: Cannot use object of type stdClass as array in /home/.... line 57
JSON (value of: $this->bck_content)
{"email":"test#email.com","membership_id":"0","fname":"Kenneth","lname":"Poulsen","userlevel":"1","created":"2012-04-23 10:57:45","lastlogin":"2012-04-23 10:58:52","active":"y"}
My code
# Display requested user details
$details_array = json_decode($this->bck_content);
$value = $details_array['email'];
print $value;
You need to use the second argument to json_decode to force array structures on JS objects.
json_decode($this->bck_content, true);
This will make sure all JS objects in the json are decoded as associative arrays instead of PHP StdObjects.
Of course that is assuming you want to use array notation to access them. If you're fine with using object notation then you can just use:
$value = $details_array->email;
try this one
$value = $details_array->email;
or
json_decode($json, true);
or
$details_array = (array)json_decode($json);
what have you done wrong is writen in error description

Categories