I have an operation like the following code:
$t = response()->json($response);
When I return $t, the following response is displayed in my browser:
{"orderId":183,"redirectUrl":"https:\/\/sandbox.domian.com\/tg\/start\/1518"}
How can I get the redirectUrl from this response?
I tried this following codes but got an error:
return $t->redirectUrl;
return json_decode($t)['redirectUrl'];
Error: Undefined property: Illuminate\Http\JsonResponse::$redirectUrl
You need to use json_decode and set the 2nd parameter to true if you want the result as an array. If you don't set the 2nd parameter, json_decode will likely return an object of type StdClass in which you can access the properties with $obj->property syntax.
http://sandbox.onlinephpfunctions.com/code/a6c62c9dbe7d343124648d18f4cb40e828634350
<?php
$json = '{"orderId":183,"redirectUrl":"https:\/\/sandbox.domian.com\/tg\/start\/1518"}';
// Notice the 2nd parameter - https://www.php.net/manual/en/function.json-decode.php#refsect1-function.json-decode-parameters
echo "Via array: " . json_decode($json, true)['redirectUrl'];
// Or
echo "\nVia object: " . json_decode($json)->redirectUrl;
Related
I am having trouble accessing information in a json-Array that I retrieved using php's json_decode function.
the json-file looks as follows:
{"code":0,"message":"Okay","model":{"results":[{"message":"Okay","balance":0,"openPositions":[[],[]],"firstDepositDate":XXX,"currencySign":"€","email":"X.X#X.com","code":0}]},"result":"success"}
I used the following php code to get the contents:
$json = file_get_contents($json_url);
$data = json_decode($json,true);
echo '<pre>' . print_r($json, true) . '</pre>';
The result print_r displays looks just like what I expect and looks like the json.
However, I cannot figure out to access the variables. Whenever I try something like
$test = $json['model']['results']['balance'];
the script throws an error, which I can't identify. I already figured out if I access the $json variable like so:
$test = $json[n]; // returns the nth character, e.g. n = 0 $test = "{", n = 2 $test = "c"
The script also did not throw an error if I tried accessing the variable like so:
$test = $json['code']; // returns "{"
Can someone help me figure out how to navigate this array?
Thanks!
The results key is a true array, not an unordered map (key/value). Additionally, you should be accessing the decoded $data, not the $json string.
This should work for you:
$test = $data['model']['results'][0]['balance'];
This is what should have tipped you off:
{"results":[{"message"
^ ^
| |
| \-- Start of an array
|
\-- Start of an object
You recieve an error similar to Cannot use object of type stdClass as array in (...) right?
json_decode returns an object of the type stdClass which is the only object in php (as far as I know) which properties cannot be accessed like an array. You will have to access them as follows:
$json = '{"code":0,"message":"Okay","model":{"results":[{"message":"Okay","balance":0,"openPositions":[[],[]],"firstDepositDate":"XX","currencySign":"€","email":"X.X#X.com","code":0}]},"result":"success"}';
$obj = json_decode($json);
var_dump($obj->message); //works
var_dump($obj["message"]); //throws exception
There is an error in the JSON array. "firstDepositDate": XXX is not a valid value. Should be a string "XXX".
Also you are trying to the wrong variable. The decoded data should be a PHP array. In this case $data['code'] instead of $json['code']
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;
I am using the following script to decode a json. Although the var_dump($obj) returns result (similar to one in here Example #1), the echo line doesn't return any result, as if the $obj being null.
<?php
$clientJSONObject = file_get_contents('php://input');
$obj = json_decode($clientJSONObject, TRUE);
var_dump($obj); // working.
echo $obj; // returns nothing.
echo $obj["carrier"]; // returns nothing.
?>
var_dump output:
array(2) {
["carrier"]=>
string(8) "Etisalat"
["userLanguage"]=>
string(2) "ar"
}
You cant echo an object property like that , you have to use -> operator
here is the example of a similar thing what you are looking for
echo $obj->{"objectname"}
will print the property name of the json decode object . and I am able to see one more error in your code . you are giving true in caps that is the reason the Jsondecode function is not giving you an array it is still throwing an object
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
I am new to PHP. I am using json_encode to convert an array into json data, and decode it using json_decode in another file. However, I am getting json error as syntax error.
My code is as follows:
File 1:
$result = get_data_array();
exit(json_encode($result));
File 2:
$result = file_get_contents("http://localhost/file1.php");
$data = json_decode($result,true);
$data->name // name is the array key
However, I am getting an error as:
Trying to get property of non-object.
You passed true to the second parameter of json_decode so it will return an array.
Use this:
$result = file_get_contents("http://localhost/file1.php");
$data = json_decode($result,true);
echo $data['name'];