Im a little stuck on parsing json output. I am working blind with json and have tried some tutorials but cant seem to find what im looking for.
I am trying to use a service providers API which is doing the functions correctly. I am then getting the correct feedback from the provider in json format.
My code so far looks like;
$response = curl_exec($apicall);
$json_output = json_decode($response);
var_dump($json_output);
This then returns;
object(stdClass)#1 (2) {
["status"]=>
string(2) "OK"
["droplet"]=>
object(stdClass)#2 (5) {
["id"]=>
int(490021)
["name"]=>
string(20) "test.mydomain.com"
["image_id"]=>
int(374535)
["size_id"]=>
int(66)
["event_id"]=>
int(6403716)
}
}
What I'm looking for is a way to store the "OK" from "status" and the "490021" from "id" as variables.
Hope someone can help.
There's no need to store them as variables. When you use json_decode, it'll create a stdClass which you can access the variables, like:
$json_output = json_decode($response);
echo $json_output->id;
Related
I have a question regarding the handling of a response from an API using the cURL function from PHP.
If I access the API via cURL I get the following response:
{"status":"ok","meta":{"count":1},"data":{"502268596":{"clan_id":500022074}}}
I transformed this code (named $json) using:
$jsondecoded = json_decode($json,true);
If I var_dump the associative array I now got I receive the following:
array(3) { ["status"]=> string(2) "ok" ["meta"]=> array(1) { ["count"]=> int(1) } ["data"]=> array(1) { [502268596]=> array(1) { ["clan_id"]=> int(500022074) } } }
My Question is: How do I access the field "clan_id"? I'm absolutely lost, because I don't have a real understanding of how arrays work and how I cycle through them.
Your response data is in array format. In this specific case, you can access clan id as follows:
echo $jsondecoded['data'][502268596]['clan_id'];
If you would rather work with objects, you can do so this way:
$jsondecoded = json_decode($json);
echo $jsondecoded->data->{502268596}->clan_id;
I am trying to go from a cur request to a page with some info.
I have the curl working but I have trouble going from the decode json to individual php variables. the conten after json_decode is:
object(stdClass)#1 (2) { ["response"]=> object(stdClass)#2 (2) { ["request"]=> string(20) "mailboxes/status/get" ["result"]=> string(1) "0" } ["status"]=> string(7) "success" }
I need the value of result which is 0 here.
Thanks in advance.
$result = $data->response->result;
Assuming the variable $data is where you stored your json_decode. It returns an instance of stdClass, and viewing the vardump, you can see the structure and get the data you want.
I'm trying to extract a value from a JSON string that's in my program. The output of var_dump($obj); is this:
object(stdClass)#1 (1) {
["BTC"]=>
object(stdClass)#2 (2) {
["value"]=>
float(403.645)
["currency"]=>
string(3) "GBP"
}
}
What I want to access is the value (currently 403.645 in this example), but I can't work out how to do it.
I've tried $obj->value and other combinations, but get nowhere; it appears to me that this is an object inside an object and as a result I can't find out how to access it. Any help would be appreciated!
I am retrieving data via JSON and PHP from a URL.
I am having difficulty breaking the object apart and displaying the data. The PHP code seems to be working until I get to the for loops.
$jsonurl = 'http://myweb/api.php';
$json = file_get_contents($jsonurl);
$json_output = json_decode($json);
foreach ($json_output->Monitoring AS $monitoring) {
foreach ($monitoring->Status AS $Status){
echo $Status->Emailed;
echo $Status->Status;
}
Here is my data structure:
object(stdClass)#12 (1)
{ ["Monitoring"]=> array(10) { [0]=> object(stdClass)#13 (14)
{
["Status"]=> string(30) "HTTP/1.1 302 Moved Temporarily"
["Emailed"]=> string(1) "0" }
[1]=> object(stdClass)#14 (14) {
["Status"]=> string(30) "HTTP/1.1 302 Moved Temporarily"
["Emailed"]=> string(1) "0" }
[2]=> object(stdClass)#15 (14) {
["Status"]=> string(30) "HTTP/1.1 302 Moved Temporarily"
["Emailed"]=> string(1) "0" }
[3]=> object(stdClass)#16 (14) {
["Status"]=> string(30) "HTTP/1.1 302 Moved Temporarily"
["Emailed"]=> string(1) "0" }
} }
According to the data structure you put, you only need one foreach loop like:
$jsonurl = 'http://myweb/api.php';
$json = file_get_contents($jsonurl);
$json_output = json_decode($json);
foreach ($json_output->Monitoring AS $Status) {
echo $Status->Emailed;
echo $Status->Status;
}
in the first foreach loop, the value ($monitoring in your code, $Status in mine as I just renamed it) is not another array that you need to loop over. It would contain a std object with Emailed and Status as keys.
First of all, are you sure your script returns actual JSON string? Did you json_encode it?
Did you compare the 'data structure' of the object before JSON and after decoding? Were there any differences? Maybe it is not at all about the foreach loops and JSON and the problem is instead in your data structure of sorts, which seems to consist of multiple sub-objects.
Alternative is to try and use associative arrays instead of objects in that context by returning json_decode($json,true) and treating it as an array.
At the outset, make sure you turn error reporting on so you can see what exactly is failing.
An improper format passed to json_decode will just return false. It is one of the limitations of PHP's json handling that they are trying to address in the next release by creating a JSONSerializable interface. Make sure the return for decode is an instance of stdClass (or an array if you have passed the option for an associative array), otherwise the loops will never execute at all.
Man this JSON thing is chewing away at my day. Is it suppose to be this difficult? Probably not. Ok, so I am receiving a URL with a json data set in it.
It looks like this:
jsonval={%22Fname%22:+%22kjhjhkjhk%22,+%22Lname%22:+%22ghghfhg%22,+%22conf[]%22:+[%22ConfB%22,+%22ConfA2%22],+%22quote%22:+%22meat%22,+%22education%22:+%22person%22,+%22edu%22:+%22welding%22,+%22Fname2%22:+%22%22,+%22Lname2%22:+%22%22,+%22gender%22:+%22B2%22,+%22quote2%22:+%22Enter+your+meal+preference%22,+%22education2%22:+%22person2%22,+%22edu2%22:+%22weld2%22,+%22jsonval%22:+%22%22}
And when I run json_decode in PHP on it, it looks like this:
object(stdClass)#1 (13) { ["Fname"]=> string(9) "kjhjhkjhk" ["Lname"]=> string(7) "ghghfhg" ["conf[]"]=> array(2) { [0]=> string(5) "ConfB" [1]=> string(6) "ConfA2" } ["quote"]=> string(4) "meat" ["education"]=> string(6) "person" ["edu"]=> string(7) "welding" ["Fname2"]=> string(0) "" ["Lname2"]=> string(0) "" ["gender"]=> string(2) "B2" ["quote2"]=> string(26) "Enter your meal preference" ["education2"]=> string(7) "person2" ["edu2"]=> string(5) "weld2" ["jsonval"]=> string(0) "" }
I guess I should mention it was encoded as a serialized object from the form page and then encoded and sent over...Don't know if that will make a difference.
Anyway, I dutifully check the PHP manual, and everything, as always, looks simple enough to implement. And then, of course, I try it just the way they tell me and I miss something that's probably obvious to everyone here but me. This bit of code, returns nothing except my text that I'm echoing:
<?php
$json = $_GET['jsonval'];
$obj = var_dump(json_decode($json));
echo "<br><br>ELEMENT PLEASE!" . $obj;
print $obj->{"Fname"}; // 12345
?>
I mean, all I want is to see the values of my individual key/values and print them out. What have I done wrong here?
Thanks for any advice.
This line is completely wrong:
$obj = var_dump(json_decode($json));
var_dump() returns nothing
You need:
$obj = json_decode($json);
Turn display_errors on in your php.ini and set up ERROR_REPORTING = E_ALL. And continue developing with such settings.
You put this:
print $obj->{"Fname"}; // 12345
It should be
print $obj->Fname; // 12345
I think you are not calling out data from the object incorrectly.
Needs to be something like:
$data = (json_decode(urldecode('{%22Fname%22:+%22kjhjhkjhk%22,+%22Lname%22:+%22ghghfhg%22,+%22conf[]%22:+[%22ConfB%22,+%22ConfA2%22],+%22quote%22:+%22meat%22,+%22education%22:+%22person%22,+%22edu%22:+%22welding%22,+%22Fname2%22:+%22%22,+%22Lname2%22:+%22%22,+%22gender%22:+%22B2%22,+%22quote2%22:+%22Enter+your+meal+preference%22,+%22education2%22:+%22person2%22,+%22edu2%22:+%22weld2%22,+%22jsonval%22:+%22%22}')));
echo $data->Fname;