如何从Laravel5中的json类型响应中获取值

时间:2022-10-24 19:35:24

Here is a function return:

这是一个函数返回:

return response()->json(['aa'=>'bbb']);  

and i print the function response ,the result like this:

我打印功能响应,结果如下:

JsonResponse {#186
 #jsonOptions: 0
 #data: "{"aa":"bbb"}"
 #callback: null
 #encodingOptions: 15
 +headers: ResponseHeaderBag {#187
 #computedCacheControl: array:1 [
  "no-cache" => true
]

i have never seen it before,how can i get the value bbb ? thanks

我以前从未见过它,我怎样才能获得价值bbb?谢谢

2 个解决方案

#1


7  

i have resolved the question,use getData() can read the json.

我已经解决了这个问题,使用getData()可以读取json。

$a = response()->json(['aa'=>'bbb']);  
$a->getData()->aa;

#2


5  

What you see is the object that response()->json() produces. That's not actually what the client will get. Because Laravel will convert it into a string before sending it back.

你看到的是response() - > json()产生的对象。这实际上并不是客户会得到的。因为Laravel会在发回之前将其转换为字符串。

On the client you can just use it as JSON. Here's an example with jQuery ajax:

在客户端上,您可以将其用作JSON。这是jQuery ajax的一个例子:

$.ajax({
    url: '/your/route'
}).done(function(data){
    alert(data.aa);  // alerts bbb
});

#1


7  

i have resolved the question,use getData() can read the json.

我已经解决了这个问题,使用getData()可以读取json。

$a = response()->json(['aa'=>'bbb']);  
$a->getData()->aa;

#2


5  

What you see is the object that response()->json() produces. That's not actually what the client will get. Because Laravel will convert it into a string before sending it back.

你看到的是response() - > json()产生的对象。这实际上并不是客户会得到的。因为Laravel会在发回之前将其转换为字符串。

On the client you can just use it as JSON. Here's an example with jQuery ajax:

在客户端上,您可以将其用作JSON。这是jQuery ajax的一个例子:

$.ajax({
    url: '/your/route'
}).done(function(data){
    alert(data.aa);  // alerts bbb
});