php - 如何在多维 JSON 字符串中包含变量?
问题描述
我正在尝试在多维 JSON 字符串中包含一个变量。我想我需要使用 json_encode 但我不知道该怎么做。
$clientID = 123456;
$body = "<h1 id='heading1'>This is just a standard html message.</h1>";
curl_setopt($ch, CURLOPT_POSTFIELDS, "{
\"subject\": \"How Did We Do\",
\"clientId\": $clientID,
\"assignedUserId\": 2988,
\"public\": false,
\"activity\": [
{
\"public\": false,
\"comment\": {
\"body\": $body
}
}
]
}");
解决方案
您应该制作一个数组并用于json_encode()
对其进行编码。
$array = [
'subject' => 'How Did We Do',
'clientId' => $clientID,
'assignedUserId' => 2988,
'public' => false,
'activity' => [
[
'public' => false,
'comment' => [
'body' => $body
]
]
]
];
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($array));
推荐阅读
- android - 如何通过 X509 证书将 Android 应用程序连接到 AWS IotCore
- javascript - Javascript问题:给定距离和方位,如何从纬度/经度地图坐标计算终端坐标
- python - 如何使用用户选择的值?
- python - 如何将 linux 命令行参数添加到 python 代码?
- sql - 在 Oracle Sql 中将 Varchar2 转换为时间
- python-3.x - Django3 ValidationError 不显示
- python - 加载 python 文件时预期的 str、bytes 或 os.PathLike 对象,未列出
- kotlin - 如何跳过流并仅发送最后发出的值
- optaplanner - 为什么 Joiners 类中没有“notEqual”方法?
- pentaho - 如何在pentaho中显示转换结果写入日志步骤?