我正在使用 PHP,为了使用 Curl 将一些数据提供给 API,我需要格式化一些字符串并且一直在使用 json_encode。它适用于更简单的位,但我无法弄清楚:
API 期望这样:
{
"id": "string",
"startTime": "2017-04-18T08:04:23.167Z",
"endTime": "2017-04-18T08:04:23.167Z",
"contacts": [
{
"id": "string",
"displayName": "string"
}
到目前为止我的代码:
$data_set_pre = array(
"id" => "",
"startTime" => "2017-04-14T07:47:59.028Z",
"endTime" => "2017-04-15T07:47:59.028Z",
"contacts" => array("id" => "ahashofsomenumbersandletters", "displayName" => "John Doe"),
);
$data_set = json_encode($data_set_pre);
不幸的是,这会产生:
{"id":"","startTime":"2017-04-14T07:47:59.028Z","endTime":"2017-04-15T07:47:59.028Z","contacts":{"id":"ahashofsomenumbersandletters","displayName":"John Doe"}}
我一直在研究,我得到的印象是 json_encode 将对所有内容进行编码,但一个从 0 开始的完整顺序数组作为对象,我现在不知道如何解决这个问题。 (不,我无法更改 API 的要求,它由第三方提供)。
最佳答案
<?php
$data_set_pre = array(
"id" => "12",
"startTime" => "2017-04-14T07:47:59.028Z",
"endTime" => "2017-04-15T07:47:59.028Z",
"contacts" => array(
array(
"id" => "ahashofsomenumbersandletters", "displayName" => "John Doe"
)
)
);
$data_set = json_encode($data_set_pre);
关于php - json_encode,[括号]中需要的关联数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43468055/