return Response::json(array('status' => 'Group not found'));
Run Code Online (Sandbox Code Playgroud)
返回受保护的数据 这是JSON:
{"status":"Group not found"}
以下代码
//$jsonData - the data returned above
var_dump($jsonData);
Run Code Online (Sandbox Code Playgroud)
返回:
object(Illuminate\Http\JsonResponse)#320(10){["jsonOptions":protected] => int(0)["data":protected] => string(28)"{"status":"未找到组"}"["callback":protected] => NULL ["encodingOptions":protected] => int(15)["headers"] => object(Symfony\Component\HttpFoundation\ResponseHeaderBag)#317(5){[ "computedCacheControl":protected] => array(1){["no-cache"] => bool(true)} ["cookies":protected] => array(0){} ["headerNames":protected] = > array(3){["cache-control"] => string(13)"Cache-Control"["content-type"] => string(12)"Content-Type"["date"] => string (4)"Date"} ["headers":protected] => array(3){["cache-control"] => array(1){[0] => string(8)"no-cache"} ["content-type"] => array(1){[0] => string(16)"application/json"} ["date"] => array(1){[0] => string(29) "星期二,2014年6月17日19:03:33 GMT"}} ["cacheControl":protected] => array(0){}} ["content":protected] => string(28)"{"status": "未找到组"}"["版本":protected] => string(3)"1.0"["statusCode":protected] => int(200)["s tatusText":protected] => string(2)"OK"["charset":protected] => NULL}
看看["data":protected]=> string(28) "{"status":"Group not found"}"
.由于某种原因,数据受到保护,并且在解码JSON时不会出现.我如何"取消保护"它(公开发布)?
小智 6
我不认为这是你的问题.
如果你看一下继承树:
\Symfony\Component\HttpFoundation\Response
\Symfony\Component\HttpFoundation\JsonResponse
\Illuminate\Http\JsonResponse
Run Code Online (Sandbox Code Playgroud)
祖先Response类具有:
public function __toString()
{
...
return ... . $this->getContent();
}
Run Code Online (Sandbox Code Playgroud)
所以我们遵循:
public function getContent()
{
return $this->content;
}
Run Code Online (Sandbox Code Playgroud)
您的数据存储在成员中是可以的,protected $content
因为当JsonResponse对象强制转换为字符串时,PHP使用该__toString()
方法的返回值作为该对象的字符串表示形式.