device_token包含方括号和空格,我想在有效载荷中删除它
这是去往供应商API的JSON负载
{
"audience": {
"device_token": "< XXXX XXXX XXXX XXXX >"
},
"device_types": [
"ios"
],
"notification": {
"ios": {
"alert": {
"title": "INSERT_TITLE_TEXT_HERE",
"body": "INSERT_BODY_TEXT_HERE"
}
}
}
}
我希望JSON具有device_token:
{
"audience": {
"device_token": "XXXXXXXXXXXXXXXX"
},
"device_types": [
"ios"
],
"notification": {
"ios": {
"alert": {
"title": "INSERT_TITLE_TEXT_HERE",
"body": "INSERT_BODY_TEXT_HERE"
}
}
}
}
答案 0 :(得分:1)
假设处理语言为javascript:
json.audience.device_token = json.audience.device_token.replace(/[<> ]/g, '')
有关更多信息,请阅读String.prototype.replace
文档:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
答案 1 :(得分:0)
首先,您将JSON对象更改为字符串,然后替换您想要的文本。
类似的东西:
// Define the payload object
var jsonPayload = {
"audience": {
"device_token": "< XXXX XXXX XXXX XXXX >"
},
"device_types": [
"ios"
],
"notification": {
"ios": {
"alert": {
"title": "INSERT_TITLE_TEXT_HERE",
"body": "INSERT_BODY_TEXT_HERE"
}
}
}
};
// Turn the object into a String
var string = JSON.stringify(jsonPayload);
// Replace the arrows and spaces with empty strings
var updatedString = string.replace(/< /g,'').replace(/ >/g,'').replace(/\s/g,'');
// Replace the jsonPayload with your new version by parsing the string
// where replacements have been made back into a JSON object;
jsonPayload = JSON.parse(updatedString);
应该可以!