Javascript 如何在php中构建此json
我使用php发送推送,但我的json对象没有得到我想要的方式 我需要我的json如下:Javascript 如何在php中构建此json,javascript,php,json,Javascript,Php,Json,我使用php发送推送,但我的json对象没有得到我想要的方式 我需要我的json如下: { "to": ["xxx"], "data": { "title": "dw", "body": "dw", "actions": [ { "icon": "emailGuests", "title": "Candidatar-me", "callback": "app.emailGuests", "foreground
{
"to": ["xxx"],
"data": {
"title": "dw",
"body": "dw",
"actions": [
{ "icon": "emailGuests", "title": "Candidatar-me", "callback": "app.emailGuests", "foreground": true},
]
}
}
$fields = array ( 'to' => $row1['fcm_registered_id'] ,
'priority' => "high",
'data' => array("title" =>$titlepost,
"body"=> $msg,
"actions" => array('icon' => 'send.ico',
'title' => 'EMAIL GUESTS',
'callback' => 'app.callbackName',
'foreground' => true
)
),
);
但我得到的却是:
{
"to": "xxx",
"data": {
"title": "dw",
"body": "dw",
"actions": {
"icon": "send.ico",
"title": "EMAIL GUESTS",
"callback": "app.callbackName",
"foreground": true
}
}
}
我正在构建我的json,如下所示:
{
"to": ["xxx"],
"data": {
"title": "dw",
"body": "dw",
"actions": [
{ "icon": "emailGuests", "title": "Candidatar-me", "callback": "app.emailGuests", "foreground": true},
]
}
}
$fields = array ( 'to' => $row1['fcm_registered_id'] ,
'priority' => "high",
'data' => array("title" =>$titlepost,
"body"=> $msg,
"actions" => array('icon' => 'send.ico',
'title' => 'EMAIL GUESTS',
'callback' => 'app.callbackName',
'foreground' => true
)
),
);
请记住,
json\u encode
将非数字索引数组转换为对象。这就是为什么要获取对象而不是对象数组。用另一个数组包装这个数组就足够了。下面是固定代码:
$fields = array (
'to' => array( $row1['fcm_registered_id'] ),
'data' => array(
"title" =>$titlepost,
"body"=> $msg,
"actions" => array(
array(
'icon' => 'send.ico',
'title' => 'EMAIL GUESTS',
'callback' => 'app.callbackName',
'foreground' => true
)
)
)
);
下面是一个工作示例:
小提示:尝试使用新的数组语法:
[]
比旧的array()
好吧,看看它:您想要的数据结构有一个名为“registration\u id”的第一级字段,所以您不认为应该在某个地方将该字段添加到您的数据结构中吗?此外,它没有“to”字段,那么为什么您认为添加该字段是个好主意呢?这个问题表明,你实际上没有尝试过任何事情来实现你描述的目标,而是希望我们为你做到。我尝试过。。。我关心的不是名字而是结构。。。这只是一个例子好吧,你在例子中看到了结构。“registration_ids”的值是一个数组-因此您应该在那里添加一个数组,而不是一个值…我的问题是actions字段。。我不知道如何在php上用[]输出它…完善了tks很多Piotr