Laravel 5 - 返回的 json 是一个简单的字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28579915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5 - Returned json is a simple string
提问by Ashik Basheer
Scenario: REST api where a client requests data from server via GET method
场景:客户端通过 GET 方法从服务器请求数据的 REST api
I am returning an array from HomeController (Server side: Laravel 5)
我从 HomeController 返回一个数组(服务器端:Laravel 5)
return ['Status' => 'Success', 'SearchResponse' => $apiresponse, 'AuthToken' => $property];
The above response is generated from a URL http://example.com/flightSearch
上面的响应是从一个 URL 生成的 http://example.com/flightSearch
On the client side (Laravel 4)
在客户端(Laravel 4)
$input=Input::all();
$url = 'http://example.com/flightSearch';
$data = array(
'client_id' => 'XXX',
'api_secret' => 'YYY',
'method'=>'SearchFlight',
'adult'=>$input['adult'],
'children'=>$input['children'],
'infant'=>$input['infant'],
'departCity'=>$input['departCity'],
'arrivalCity'=>$input['arrivalCity'],
'departDate'=>$input['departDate'],
'returnDate'=>$input['returnDate'],
'journeyType'=>$input['journeyType']
);
$params = http_build_query($data);
$result = file_get_contents($url.'?'.$params);
$response = json_decode($result);
return $response->Status //Works
return $response->AuthToken //Works
return $response->SearchResponse //Throws following Error
Error:
错误:
The Response content must be a string or object implementing __toString()
Solution:
解决方案:
The variable $apiresponse
was an object returned from a remote server. Adding the variable to an object solved the problem
该变量$apiresponse
是从远程服务器返回的对象。将变量添加到对象解决了问题
return ['Status' => 'Success', 'SearchResponse' => array($apiresponse), 'AuthToken' => $property];
回答by lukasgeiter
Update
更新
Since you have a JSON string you can simply use json_decode()
:
由于您有一个 JSON 字符串,您可以简单地使用json_decode()
:
$response = json_decode($result);
return $response->Status;
The Response content must be a string or object implementing __toString()
响应内容必须是实现 __toString() 的字符串或对象
This is just because you're returning the $response->SearchResponse
from your controller action. Using it like $response->SearchResponse->SomeProperty
will just work fine. No need for array($apiresponse)
If you want to see all the contents of that variable use var_dump()
:
这只是因为您正在$response->SearchResponse
从控制器操作中返回。像使用它一样$response->SearchResponse->SomeProperty
可以正常工作。不需要array($apiresponse)
如果您想查看该变量的所有内容,请使用var_dump()
:
var_dump($response->SearchResponse);
Assuming you created the $response
with Laravels help this should be an instance of Illuminate\Http\JsonResponse
.
假设您创建了$response
with Laravel help 这应该是Illuminate\Http\JsonResponse
.
You can get the data (already decoded) with getData()
:
您可以使用以下命令获取数据(已解码)getData()
:
$data = $response->getData();
echo $data->Name
回答by SirajuddinLuck
$test1 = array('name'=>'gggg');
print_r($test1); //answer: Array ([name]=>gggg)
$test2 = json_encode($test1);
print_r($test2); //answer: {"name":"gggg"}
$test3 = json_decode($test2);
echo $test3->name; //answer: gggg