【问题标题】:Associative json array with curl带有 curl 的关联 json 数组
【发布时间】:2016-10-16 13:10:12
【问题描述】:

我需要发送这个数组:

$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} [] []

请帮忙。

【问题讨论】:

    标签: php json curl


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      据我了解,通过 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
                      }
              ]
      }
      

      【讨论】:

      • {"message":"内部错误。详细信息可在 Magento 日志文件中找到。报告 ID:webapi-58037ddbaccd0"}
      • {"message":"内部错误。详细信息可在 Magento 日志文件中找到。报告 ID:webapi-58037ddbaccd0"} 尝试使用 '[]' 括号时我得到的结果。日志:下一个异常:报告 ID:webapi-58037ddbaccd0;消息:注意:/home/workuser/Projects/magenov/vendor/magento/framework/Reflection/TypeProcessor.php 第 505 行中的数组到字符串转换
      • @IlliaArefyev:无法帮助您使用 Magento。只能告诉你上面是 A) 有效的 JSON,和 B) 用 nums 属性表示对象的正确方法,它是一个对象数组。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-12
      • 1970-01-01
      • 2011-05-26
      • 1970-01-01
      • 2011-11-26
      • 2015-01-14
      相关资源
      最近更新 更多