i am new to JSON i have a json object retrieved from the database in the form of
Array
(
[0] => stdClass Object
(
[id] => 1
[data] => {"vehicle":[{"year":"2000","make":"Ac","model":"Aceca","acquired_year":"2016","acquired_month":"2","use":"Business","distance_driven_to_work_or_school":"2","distance_driven_manually":"10000"}],"first_name":"ADAS","last_name":"DSADSADA","email":"asddsa#sda.com","phone":"dsasasa","postal_code":"","drivers":[{"name":"ssada","birth_year":"2016","birth_month":"2","birth_day":"2","gender":"female","martial_status":"Single","license_number_provided":"yes","license_number":"asddasdas","license_type":"","training_completed":"","years_been_listed_on_auto_policy_in_north_america":"No Previous Experience","license_suspensions":"","accidents":"Select","convictions":"Select","cancellation_reason":"","cancellation_year":"","cancellation_month":"","cancellation_day":""}],"considering_renters_to_reduce_rate":"yes","install_winter_tires":"no","park_in_private_driveway":"yes","willing_to_install_device":"no","years_insured_with_current_company":"4 Years","how_heard_about_us":"asdaa"}
[date] => 2017-11-20 18:17:52
[status] => 0
)
)
now when i try to use json_decode to convert it into an array i am getting Trying to get property of non-object here's my code
<?php
echo "<pre>";
print_r($quotes); //works fine uptil here
$data = json_decode($quotes->data,true);//the error line
echo "<pre>";
print_r($data);
?>
i tried it a couple of ways but it is not working i tried some other solutions as well stil ending up getting errors any help?
It is because $quotes is an array of objects. Try $quotes[0]->data, e.g.:
$data = json_decode($quotes[0]->data,true);
// ------------------------^^^
You're receiving an array containing objects from the database. You're almost there but instead of
$data = json_decode($quotes->data,true);
You should use
$data = json_decode($quotes[0]->data,true);
Related
I'm using PHP SoapClient to call a SOAP service.
I'm using the following code to return the below view
$response = $client->Get($request);
echo '<pre>';
print_r($response);
echo '</pre>';
echo '<hr>';
Here's the response
stdClass Object
(
[TransactionID] => 17ACE7B75CB6SDBX
[ResponseType] => SYNC
[Parameters] => stdClass Object
(
[Param] => stdClass Object
(
[_] => SANDBOX20101001123321125.168.214.72125.168.253.29912003D60E04CC4C1F8
[id] => SessionLogRecord
)
)
)
Here is , showing the elements within it that I want to extract the values of
<onlinesessionrecord><serviceid>SANDBOX</serviceid><datetime>20101001123321</datetime><ipaddress>125.168.214.72</ipaddress><nasipaddress>125.168.253.2</nasipaddress><nasport>9912</nasport><sessionid>003D60E04CC4C1F8</sessionid></onlinesessionrecord>
For example, I am trying to extract from above
I have tried to display it using the following, but I get an error "Attempt to read property "onlinesessionrecord" on string"
$data = $response->Parameters->Param->_->onlinesessionrecord->ipaddress;
print_r($data);
If anyone knows what I'm doing wrong, your advice would be appreciated.
I am sure this is very simple, I just seem to struggle with xml.
--- Next Day, further findings after help from comments ---
So, thanks to the comments below, I have now go to this point. If I use the below:-
$data = simplexml_load_string($response->Parameters->Param->_);
$out = $data->IPAddress; // same if I do the following $data[0]->IPAddress;
print_r($out);
I get this:-
SimpleXMLElement Object ( [0] => 125.168.214.72 ) if I do the below.
But I just want to extract the IPAddress value and assign it to a variable. I have tried 2 ways
$out = $data->IPAddress;
and
$out = $data[0]->IPAddress;
Both give the same output, I just want to actual IPAddress and assign it to a variable.
i am trying to iterate a result set using foreach loop. Below is my array when i do print_r on result set
Array
(
[0] => stdClass Object
(
[xml] =>
[qid] => 406
[title] => Q by Sikander
[description] => test question created by Sikander
)
)
Using following foreach loop
foreach ($xmls_nodes as $value){
echo $value->qid ;
echo $value->xml ;
echo $value->title ;
echo $value->description ;
}
now as a result it prints following
406 Q by Sikander test question created by Sikander
but right after this it also prints following for each of the record and row
A PHP Error was encountered Severity: Notice
Message: Trying to get property 'qid' of non-object
it give same line no in for error where i am printing these values and using die statement after this, please help me to get rid of this issue
You have to convert this object to array and then just use it as array:
$arr = json_decode(json_encode($xmls_nodes), true);
You have to do JSON-encode your object and then decode it back to an array
[0] => stdClass Object
$arr = json_decode(json_encode($xmls_nodes), true);
then loop through it
I need one help. I am unable to convert string to json array using PHP. I am explaining my code below.
$education=$_POST['education'];
the above line give this output [{'name':'aaa','id':'12'},{'name':'bbb','id':'20'}]. But its coming as a string .I tried to convert into array like below but it gived output as null
$edu=json_decode($education,true);
print_r($edu);
It gives the empty output. Here I need to convert it to array and populate all data. Please help me.
Hi You need to make your json string like below:
$arr = '[{"name":"aaa","id":"12"},{"name":"bbb","id":"20"}]';
$a = json_decode($arr);
echo "<pre>";
print_r($a);
die;
it will show your output like below:
Array
(
[0] => stdClass Object
(
[name] => aaa
[id] => 12
)
[1] => stdClass Object
(
[name] => bbb
[id] => 20
)
)
In php, you can use this function to check valid json:
function _isJsonString($string) {
json_decode($string);
return (json_last_error() == JSON_ERROR_NONE);
}
You can also check online for valid json: http://json.parser.online.fr/
Hope this will help.
I've experienced a problem when developing for a minecraft server: I have an array like this which is received as json and encoded but for processing the output of this I need to know how to access the array and echo one of the values for Example the Version Key.I tried to save the array as variable $json and to access it via echo $json->Version; but this results in the output of this error: Notice: Trying to get property of non-object in /home/bs-web/joomla/api/test2.php on line 12
HereĀ“s the Array:
Array
(
[0] => stdClass Object
(
[GameName] => MINECRAFT
[Version] => 1.8
[Plugins] =>
[Map] => BungeeCord_Proxy
[Players] => 7
[MaxPlayers] => 100
[HostPort] => 25565
[HostIp] => 188.40.97.86
[Software] => Vanilla
)
)
Your variable $json is an array, you cannot get property of an array. Assuming you have only one element in the array as in your example, you should use
echo $json[0]->Version;
to display the version(1.8 in your example).
once you saves the $json variable as an array, you only can access its values passing the key that you want to use, and then, point to the object. In your case, to get the Version property, you will need to use $json[0]->Version.
To access and echo all values of your object you can use a foreach loop to iterate with it.
foreach ($json as $object){
echo $object->GameName;
echo $object->Version;
echo $object->Plugins;
.... // All properties that you want
}
Hope it helps you, hugs.
You could also use json_decode($json, true) then you get an assocative array which you can access like this $json['Version']
I'm trying to use JSON decode to retrieve some information but it's not working, it's just showing the data as null when I use var_dump
Here's the JSON formatted data passed in the URL
orderSummary={"orderInfo":[{"itemNumber":"1","quantity":"3","price":"5.99","productName":"Item_B"}]}
When I simply echo the un-decoded string I get the following
echo $_GET['orderSummary'];
//displays the following
{\"orderInfo\":[{\"itemNumber\":\"1\",\"quantity\":\"3\",\"price\":\"5.99\",\"productName\":\"Item_B\"}]}
However, when I try to decode it the result is null
$order = $_GET['orderSummary'];
$data = json_decode($order,true);
echo "<PRE>";
var_dump($data); die();
//displays the following
<PRE>NULL
Is it not properly formatted?
Run the input string through stripslashes() first.
$input = '{\"orderInfo\":[{\"itemNumber\":\"1\",\"quantity\":\"3\",\"price\":\"5.99\",\"productName\":\"Item_B\"}]}';
print_r(json_decode(stripslashes($input)));
Output
stdClass Object
(
[orderInfo] => Array
(
[0] => stdClass Object
(
[itemNumber] => 1
[quantity] => 3
[price] => 5.99
[productName] => Item_B
)
)
)
Demo
Alternatively
Turn off magic_quotes_gpc. Considering that it has been deprecated (and removed in 5.4), this the better option.