Accessing Object Properties returned from ASP.net - php

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;

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

json_decode() getting values

I am trying to get json data from the Statcounter API.
I have the following:
$query = makeQuery("2292634", "demo_user", "statcounter", "visitor", "");
echo $query . "<br>";
$response = file_get_contents($query, true);
$data = json_decode($response, true);
echo $data['sc_data']['log_visits'];
I know the query is correct, and I know the $response is filled with unformatted json. The trouble is accessing the array and pulling the values out.
Here are the first couple lines of the unformatted json it is giving me.
This link will only work for 15 minutes, I can generate a new one if you would like to see the raw json.
http://api.statcounter.com/stats/?vn=3&s=visitor&pi=2292634&t=1398791335&u=demo_user&sha1=c6cdfd6c84227801c6ca758c17252712e3f76514
{"#attributes":{"status":"ok"},"sc_data":[{"log_visits":"1","entries_in_visit":"2","entry_t":"2014-04-29 17:57:33","entry_url":"http:\/\/www.guitar-online.com\/en\/","entry_title":"Learn how to play the guitar: tutorials, guitar
Obviously I am not accessing the array in the correct way...but I haven't found the syntax to make it work YET!
Thank you for your help.
Looking at your data, sc_data is an array containing nested JSON objects so you should be able to iterate over that array to read the data like you want:
echo $data['sc_data'][0]['log_visits'];
The code above will access the first element of the array sc_data and print the value of log_visits
When you have {"field":['a','b','c']} in JSON, you would access 'a' as field[0].
The elements of the array can be any valid value, i.e.
string
number
object
array
true
false
null
In your case it is objects and you access that object the same way you would access any other array element - by the index number (JSON doesn't have associative array of type "key" => "value")

how to echo object or hash type field php

Please how do I echo or obtain the values of a field of type object?
I first encoded, replaced string and decoded the code as below:
$mongorow = json_encode($mongorow);
$mongorow= preg_replace("/_DOT_/", ".", $mongorow);
$mongorow = json_decode($mongorow);
And then in my form I'm trying to use the fields as below:
value="<?php echo $mongorow->name;?>" //this works producing value of name
value="<?php echo $mongorow->properties;?>" //this produces an error where properties is of type object
Error message I get is (symfony)
Catchable Fatal Error: Object of class stdClass could not be converted to string in ...
First try to debug the type of the $mongorow->properties you can debug the object by using the var_dumb function.
Try this:
var_dump($mongorow);
And you will get the exact data.
For more read here about the var_dump.
You can use var_dump() or print_r() function to display the object or hashtype field
echo "<pre>"; var_dump($mongorow);
print_r($mongorow);
If you want to assign object to the variable as a string use:
serialize($obj);
If you want to just dump value on screen use:
var_dump($obj);`

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