具有curl的关联json数组

时间:2016-10-16 13:10:13

标签: php 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
            }
    ]}

我有例外:

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): Magento\Framework\Webapi\ErrorProcessor->_critical(Object(Exception))
#1 /home/workuser/Projects/magentov/vendor/magento/module-webapi/Controller/Rest.php(219): Magento\Framework\Webapi\ErrorProcessor->maskException(Object(Exception))
#2 /home/workuser/Projects/magentov/var/generation/Magento/Webapi/Controller/Rest/Interceptor.php(37): Magento\Webapi\Controller\Rest->dispatch(Object(Magento\Framework\App\Request\Http))
#3 /home/workuser/Projects/magentov/vendor/magento/framework/App/Http.php(135): Magento\Webapi\Controller\Rest\Interceptor->dispatch(Object(Magento\Framework\App\Request\Http))
#4 /home/workuser/Projects/magentov/vendor/magento/framework/App/Bootstrap.php(258): Magento\Framework\App\Http->launch()
#5 /home/workuser/Projects/magentov/pub/index.php(37): Magento\Framework\App\Bootstrap->run(Object(Magento\Framework\App\Http))
#6 {main} [] []

请帮助。

2 个答案:

答案 0 :(得分:0)

在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
                    }
            ]
    }

答案 1 :(得分:0)

  

据我了解,我无法使用' []'通过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
                }
        ]
}