Laravel数据集解析错误

时间:2017-10-18 09:31:43

标签: laravel laravel-5.4

我需要解析这个数据集,但我尝试了几种方法,但它不起作用。

{"id":2,"area_id":"1","payment_term_id":"1","payment_type_id":"1","trade_channel_id":"1","salesman_id":1,"customer_name":"xxx","contact_number":"1231212","fax_number":"1231232","home_address":"asd","delivery_address":"dasd","email_address":"test@edit.com","created_by":"4","updated_by":"4","is_active":"1","created_at":"2017-10-17 06:02:59","updated_at":"2017-10-17 06:13:22"}

我试过这个,但是这个错误说明了

(1/1)ErrorException 试图获得非对象的属性

$details = $request->get('details');
return $details->id;

我也试过这个

$details = $request->get('details');
return $details['id'];

(1/1)ErrorException 非法字符串偏移' id'

我正在使用laravel 5.4

3 个答案:

答案 0 :(得分:1)

由于数据来自请求,因此它是JSON而不是集合或对象。所以,你需要做这样的事情:

return json_decode($request->details)->id;

答案 1 :(得分:1)

要在控制器中检索JSON有效内容,请使用:

$details = json_decode($request->details)->id);
dd($details);

而不是

$details = $request->get('details');
return $details['id'];

答案 2 :(得分:1)

要像$details['id']一样使用数组,请尝试:

$details = json_decode($request->details, true);
echo $details['id'];