You get the error because you are trying to turn a stdClass object into a string, something it doesn't support.
Instead of echo $decoded_traces try var_dump($decoded_traces) - that will give a diagnostic view of the object you've decoded (which I presume is what you wanted). You should find it looks like this
class stdClass#1 (2) {
public $Coords =>
array(5) {
[0] =>
class stdClass#2 (4) {
public $Accuracy =>
string(2) "65"
public $Latitude =>
string(18) "53.277720488429026"
public $Longitude =>
string(18) "-9.012038778269686"
public $Timestamp =>
string(39) "Fri Jul 05 2013 11:59:34 GMT+0100 (IST)"
}
[1] => (more of the same, edited for brevity)
[2] =>
[3] =>
[4] =>
}
public $Scan =>
string(8) "Whatever"
}
By default, json_encode will create stdClass objects like the above. If you would prefer an associative array, pass true as the second parameter to json_decode, e.g. $decoded_traces=json_decode($traces, true);
As a further aside, although stdClass can't be turned into a string, your own classes can - a class which implements the __toString magic method could be converted to a string!
Answer from Paul Dixon on Stack OverflowHi all I have the following code https://pastebin.com/Emsgz88u
I've managed to get the data back from graphiql, however I cannot process the object. test2 at the bottom, I end up with a error Object of class stdClass could not be converted to string
I believe that I'm not handling the returned object correctly.
var_dump of one of the objects is
stdClass Object ( [name] => 20XX-LWxxxx:Law of xxxxxxxx ) object(stdClass)#3 (1) { ["name"]=> string(27) "20XX-LWxxxx:Law of xxxxxxxx" }
I've tried the following https://stackoverflow.com/questions/28098/php-equivalent-of-net-javas-tostring
and I've been following below to process the JSON coming back from graphiql https://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php
Hope someone will help....Thanks... P
You get the error because you are trying to turn a stdClass object into a string, something it doesn't support.
Instead of echo $decoded_traces try var_dump($decoded_traces) - that will give a diagnostic view of the object you've decoded (which I presume is what you wanted). You should find it looks like this
class stdClass#1 (2) {
public $Coords =>
array(5) {
[0] =>
class stdClass#2 (4) {
public $Accuracy =>
string(2) "65"
public $Latitude =>
string(18) "53.277720488429026"
public $Longitude =>
string(18) "-9.012038778269686"
public $Timestamp =>
string(39) "Fri Jul 05 2013 11:59:34 GMT+0100 (IST)"
}
[1] => (more of the same, edited for brevity)
[2] =>
[3] =>
[4] =>
}
public $Scan =>
string(8) "Whatever"
}
By default, json_encode will create stdClass objects like the above. If you would prefer an associative array, pass true as the second parameter to json_decode, e.g. $decoded_traces=json_decode($traces, true);
As a further aside, although stdClass can't be turned into a string, your own classes can - a class which implements the __toString magic method could be converted to a string!
use json_encode($traces) this will convert the array into the string. json_decode() is used to convert a string into array or object array
php - Catchable fatal error: Object of class stdClass could not be converted to string - Stack Overflow
php - json_encode error Object of class stdClass could not be converted to string - Stack Overflow
php - I have issue with decoding json ex. '{"a":"b"}' - WordPress Development Stack Exchange
php - problems with JSON Decode & strings - Stack Overflow
use true as second parameter in json_decode
$arr = json_decode($your_array,true); // it will create array
working example http://codepad.viper-7.com/0Dxxm8
This is valid json and working too It is already a json so no need to parse this json
var json={};//copy json from http://pastebin.com/rLw4ABwS
console.log(json);
Test the code on fiddle http://jsfiddle.net/rRNSN/
Check in console it returns as an object means it is valid json,
If it will be a string then it needs to be parse using JSON.parse() or by jquery.parseJSON().
The function json_decode does not return a string.
Have a look at the documentation for the function.
https://www.php.net/function.json-decode.php
The problem is that you're trying to echo the object. The following code is faulty.
echo $issue_json;
You can use var_dump to see the structure of the object returned from json_decode.
var_dump($issue_json);
To answer the question title, if your object does not have a __string method using it as a string will throw an exception. (I think this changed in 5.2), so to do unexploding string conversion on the hop you need:
if (is_object($arg)) {
$arg = method_exists($arg, '__toString') ? (string)$arg : 'Object';
}
You see, the json_decode() method doesn’t return our JSON as a PHP array; it uses a stdClass object to represent our data. Let’s instead access our object key as an object attribute.
echo $stateList->AL;
If you provide true as the second parameter to the function we will receive our PHP array exactly as expected.
$stateList = json_decode($states,true);
echo $stateList['AL'];
Either you pass true to json_decode like Nanhe said:
$stateList = json_decode($states, true);
or change the way you access it:
echo $stateList->{'AL'};
Use the second parameter of json_decode to make it return an array:
$result = json_decode($data, true);
The function json_decode() returns an object by default.
You can access the data like this:
var_dump($result->context);
If you have identifiers like from-date (the hyphen would cause a PHP error when using the above method) you have to write:
var_dump($result->{'from-date'});
If you want an array you can do something like this:
$result = json_decode($json, true);
Or cast the object to an array:
$result = (array) json_decode($json);
$price1 = DB::table('dishes')->select('price', 'chg_porridge')->where([
['name', '=', $opt1],
])->get();
$values = json_decode(json_encode($price1), true);
You can avoid doing all that by using the method value()
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Menu $menu
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $day)
{
$opt1 = $request->post('dishopt1');
$price1 = DB::table('dishes')->where([
['name', '=', $opt1],
])->value('price');
echo $price1;
}
try
var_dump($json);
This allows you to print out the details of objects and other non-primative types.
Echoing is used for strings - Your json decoded string will be an object of type stdClass
See var_dump http://php.net/manual/en/function.var-dump.php
See echo http://php.net/manual/en/function.echo.php
You have to pass the second parameter of json_decode as true, more info about these parameters at: http://php.net/manual/en/function.json-decode.php
so your echo json_decode($json); should be changed to this:
print_r(json_decode($json, true));
echo is changed to print_r since the output is an array not a string ...