Trying to get multi sub-level JSON using Stripe API [duplicate] - 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

Accessing an object in php

Currently, I'm trying to move away from mySQLi procedural to object oriented style. So, I passed an object to a php file via the Jquery Post() function.
Below:
JS file
var newemployer = {
newemployeremail:newemployeremail,
newcompanyname: newcompanyname,
secondpassword:secondpassword,
};
var employerdata = JSON.stringify(newemployer);
console.log("in jquery: " + employerdata);
$.post('api/employerprocess.php', {employerdata:employerdata}, function(data){
console.log(data);
});
On the PHP side: I have this:
$employerdata = $_POST['employerdata'];
$data = json_decode($employerdata, true);
print_r($data['newemployeremail']);
I can access the object in the above, but I can't seem to get access to the values in the object using the -> operator.
$accessed = $data->newemployeremail;
var_dump($data->newemployeremail);
echo $data->newemployeremail;
echo $accessed;
echo 'Hi';
The $accessed variable, var_dump, the echoing of the object each yield this error, last two statements have a NULL at the end:
Trying to get property of non-object
I'd like to use the -> operator. I don't get it. Why can't I access the values in the object with the -> operator?
true as a second argument to json_decode() means "returned objects will be converted into associative arrays". Hence you're receiving array, not an object.
You need to either pass false to second argument to receive object or access properties using array syntax: $accessed = $data['newemployeremail'];
You are assigning the values as an associative array
$data = json_decode($employerdata, true);
// the true param in json_decode mean that teh result is converted in associative array
echo $data['newemployeremail'];
so you have an array and not an object
then try assign the json decode without true eg:
$data = json_decode($employerdata);
echo $data->newemployeremail;

Notice: Trying to get property of non-object in

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 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;

PHP: Illegal string offset

I know there already are questions like this, but It didn't help me.
I get the follow error on my site:
Warning: Illegal string offset 'networkConnections' in
/var/www/bitmsg/templates/header.php on line 25 {
The line is
<?= $bmstatus["networkConnections"] ?> p2p nodes
if I print_r $bmstatus, then I get:
{
"numberOfBroadcastsProcessed": 2308,
"networkStatus": "connectedAndReceivingIncomingConnections",
"softwareName": "PyBitmessage",
"softwareVersion": "0.4.1",
"networkConnections": 52,
"numberOfMessagesProcessed": 22888,
"numberOfPubkeysProcessed": 8115
}
How to I fetch the information from this array?
I've tried both $bmstatus['networkConnections'] and $bmstatus->networkConnections
but both is returning that error?
$bmstatus contains a JSON string. You have to decode it first to be able to extract the required information out of it. For this purpose, you can use the built-in function json_decode() (with the second parameter set as TRUE to get an associative array, instead of an object):
$json = json_decode($bmstatus, true);
echo $json['networkConnections'];
It's a json string. You need to decode your json response using json_decode with second parameter true to get as an associative array.
$bmstatusArray = json_decode($bmstatus,true);
echo $bmstatusArray["networkConnections"];

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