关联json数组与curl



我需要发送这个数组:

$nums = [
            [
                'title' => 'How to create a simple module',
                'summary' => 'The summary',
                'description' => 'The description',
                'created_at' => date('Y-m-d H:i:s'),
                'status' => 1
            ],
            [
                'title' => 'Create a module with custom database table',
                'summary' => 'The summary',
                'description' => 'The description',
                'created_at' => date('Y-m-d H:i:s'),
                'status' => 1
            ]
        ];

的JSON使用curl我试试这个:

{"nums":
        {
                {
                 "title":"How to create a simple module",
                 "summary":"The summary",
                 "description":"The description",
                 "created_at":"2016-10-16 12:51:26",
                 "status":1
                },
                {
                 "title":"How to create a simple module",
                 "summary":"The summary",
                 "description":"The description",
                 "created_at":"2016-10-16 12:51:26",
                 "status":1
                }
        }
}

但是我得到一个解码错误。

当我发送的时候

{"nums":
            {
                    {
                     "title":"How to create a simple module",
                     "summary":"The summary",
                     "description":"The description",
                     "created_at":"2016-10-16 12:51:26",
                     "status":1
                    }
            }
    }

行得通,我得到

Array(
[title] => How to create a simple module
[summary] => The summary
[description] => The description
[created_at] => 2016-10-16 12:51:26
[status] => 1)

但这不是我需要的。

据我所知,当curl发送JSON数组时,我不能使用'[]'括号,因为当我试图发送

{"nums":
    [
            {
             "title":"How to create a simple module",
             "summary":"The summary",
             "description":"The description",
             "created_at":"2016-10-16 12:51:26",
             "status":1
            },
            {
             "title":"How to create a simple module",
             "summary":"The summary",
             "description":"The description",
             "created_at":"2016-10-16 12:51:26",
             "status":1
            }
    ]}

I've got exception:

Next Exception: Report ID: webapi-58037ddbaccd0; Message: Notice: Array to string conversion in /home/workuser/Projects/magentov/vendor/magento/framework/Reflection/TypeProcessor.php on line 505 in /home/workuser/Projects/magentov/vendor/magen$
Stack trace:
#0 /home/workuser/Projects/magentov/vendor/magento/framework/Webapi/ErrorProcessor.php(139): MagentoFrameworkWebapiErrorProcessor->_critical(Object(Exception))
#1 /home/workuser/Projects/magentov/vendor/magento/module-webapi/Controller/Rest.php(219): MagentoFrameworkWebapiErrorProcessor->maskException(Object(Exception))
#2 /home/workuser/Projects/magentov/var/generation/Magento/Webapi/Controller/Rest/Interceptor.php(37): MagentoWebapiControllerRest->dispatch(Object(MagentoFrameworkAppRequestHttp))
#3 /home/workuser/Projects/magentov/vendor/magento/framework/App/Http.php(135): MagentoWebapiControllerRestInterceptor->dispatch(Object(MagentoFrameworkAppRequestHttp))
#4 /home/workuser/Projects/magentov/vendor/magento/framework/App/Bootstrap.php(258): MagentoFrameworkAppHttp->launch()
#5 /home/workuser/Projects/magentov/pub/index.php(37): MagentoFrameworkAppBootstrap->run(Object(MagentoFrameworkAppHttp))
#6 {main} [] []

帮助。

在JSON数组中,项必须用方括号括起来

{"nums":
            [
                    {
                     "title":"How to create a simple module",
                     "summary":"The summary",
                     "description":"The description",
                     "created_at":"2016-10-16 12:51:26",
                     "status":1
                    },
                    {
                     "title":"How to create a simple module",
                     "summary":"The summary",
                     "description":"The description",
                     "created_at":"2016-10-16 12:51:26",
                     "status":1
                    }
            ]
    }

据我所知,当curl发送JSON数组时,我不能使用'[]'括号。

无论你从哪里得到这种理解,它都是错误的。见http://json.org。[]正是您在JSON中定义nums值所需要的,它应该看起来像这样:

{"nums":
        [
                {
                 "title":"How to create a simple module",
                 "summary":"The summary",
                 "description":"The description",
                 "created_at":"2016-10-16 12:51:26",
                 "status":1
                },
                {
                 "title":"How to create a simple module",
                 "summary":"The summary",
                 "description":"The description",
                 "created_at":"2016-10-16 12:51:26",
                 "status":1
                }
        ]
}

最新更新