我想在使用 json_decode
方法解码之前删除键或对象名称之间的所有空格。
像 Income cut
我可以做到 Income_cut
吗?
有没有办法解决这个问题。
{
"Housing": 0,
"Late Comers": 0,
"Income cut": "12500",
"Study Allowance": 0,
"test": 0,
"i": 0,
"staff_no": "9",
"staff_name": "Abeja Vicky",
"staff_department": "Production Staff",
"staff_position": "Production Manager",
"staff_salary": "0",
"GrossValue": 12500,
"GrossSalary": 0,
"NSSF": 0,
"PAYE": 0,
"GrossValueAddiotion": 0,
"GrossValueDecuction": 12500,
"netPay": -12500,
"Balance": 0,
"Paidx": 0,
"balance": -12500
}
最佳答案
假设没有嵌套简单的 json 对象,下面将在发送到服务器之前工作
var input ={
"Housing": 0,
"Late Comers": 0,
"Income cut": "12500",
"Study Allowance": 0,
"test": 0,
"i": 0,
"staff_no": "9",
"staff_name": "Abeja Vicky",
"staff_department": "Production Staff",
"staff_position": "Production Manager",
"staff_salary": "0",
"GrossValue": 12500,
"GrossSalary": 0,
"NSSF": 0,
"PAYE": 0,
"GrossValueAddiotion": 0,
"GrossValueDecuction": 12500,
"netPay": -12500,
"Balance": 0,
"Paidx": 0,
"balance": -12500
}
var output = {};
for(var key in input){
output[key.replace(/\s+/,'_')] = input[key];
}
console.log(output);
关于javascript - 如何删除对象的键和值之间的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35541239/