如何处理Laravel 5中的嵌套JSON对象请求?

时间:2022-10-24 13:56:49

We had this webservice running in Laravel 5 and AngularJs/Ionic for handling the web. When we post request from the web (client) to webservice (backend), we passed nested JSON object. We are having an issue to read all child objects under parent object in the server side.

我们的web服务运行在Laravel 5和AngularJs/Ionic中,用于处理web。当我们将请求从web(客户端)发送到webservice(后端)时,我们传递了嵌套的JSON对象。我们有一个问题,读取服务器端父对象下的所有子对象。

{
    "name": "test",
    "description": "test",
    "startdate": "2016-02-21T13:00:00.000Z",
    "enddate": "2016-02-23T13:00:00.000Z",
    "coach": {
        "uuid": "76fdd664-d830-11e5-9d46-00ffc9587cbc"
    },
    "category": {
        "uuid": "771e6de4-d830-11e5-9d46-00ffc9587cbc"
    },
    "useruuid": "76d65a2d-d830-11e5-9d46-00ffc9587cbc",
    "routines": ["775b2726-d830-11e5-9d46-00ffc9587cbc"]
}

This JSON has been verified ok and I also we managed to get the basic one such as name, endate etc etc BUT not the nested object one.

这个JSON已经被验证过了,我也得到了基本的,比如名字,endate等等,但是没有嵌套对象。

We are using something like this in Laravel 5:

我们在Laravel 5中使用了类似的东西:

$incomingdata = $request->json()->all();
$name = $incomingdata->name; // works
$startdate = $incomingdata->startdate; // works
$coach_uuid = $incomingdata->coach()->uuid; // didn't work !!!

How do I achieve this?

我该如何做到这一点?

2 个解决方案

#1


1  

You can try this:

你可以试试这个:

$phpArray = json_decode($jsonObject,true);
$name = $phpArray['name'];
$coach = $phpArray['coach'];
$coach_uuid = $phpArray['coach']['uuid'];
$category = $phpArray['category']; 
$category_uuid = $phpArray['category']['uuid]; 

#2


0  

I don't know about Laravel 5.0, but in Laravel 5.6 I had to do something quite different. None of the code in other answers worked for me.

我不知道Laravel 5.0,但在Laravel 5.6中,我必须做一些完全不同的事情。其他答案中的代码对我都不起作用。

Here is what I found to work correctly:

以下是我发现正确工作的地方:

$name = $request->input('name');
$startdate = $request->input('startdate');
$coach_uuid = $request->input('coach.uuid');
print_r($name.'<br />');
print_r($startdate.'<br />');
print_r($coach_uuid.'<br />');

The above prints:

上面的输出:

test
2016-02-21T13:00:00.000Z
76fdd664-d830-11e5-9d46-00ffc9587cbc

Reference: https://laravel.com/docs/5.6/requests#retrieving-input

参考:https://laravel.com/docs/5.6/requests # retrieving-input

#1


1  

You can try this:

你可以试试这个:

$phpArray = json_decode($jsonObject,true);
$name = $phpArray['name'];
$coach = $phpArray['coach'];
$coach_uuid = $phpArray['coach']['uuid'];
$category = $phpArray['category']; 
$category_uuid = $phpArray['category']['uuid]; 

#2


0  

I don't know about Laravel 5.0, but in Laravel 5.6 I had to do something quite different. None of the code in other answers worked for me.

我不知道Laravel 5.0,但在Laravel 5.6中,我必须做一些完全不同的事情。其他答案中的代码对我都不起作用。

Here is what I found to work correctly:

以下是我发现正确工作的地方:

$name = $request->input('name');
$startdate = $request->input('startdate');
$coach_uuid = $request->input('coach.uuid');
print_r($name.'<br />');
print_r($startdate.'<br />');
print_r($coach_uuid.'<br />');

The above prints:

上面的输出:

test
2016-02-21T13:00:00.000Z
76fdd664-d830-11e5-9d46-00ffc9587cbc

Reference: https://laravel.com/docs/5.6/requests#retrieving-input

参考:https://laravel.com/docs/5.6/requests # retrieving-input