如何删除键和对象值之间的空格 [英] How can I remove white spaces between keys and values of object

查看:118
本文介绍了如何删除键和对象值之间的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在使用 json_decode 方法解码之前删除键之间的所有空格或对象的名称。

I want to remove all the white spaces between the keys, or names of the objects before I decode them using json_decode method.

喜欢收入减少我可以让它 Income_cut

有没有办法解决这个问题。

Is there a way around this.

{
    "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
}


推荐答案

另一种选择

一些发布的解决方案在测试时失败,特别是当输入略有不同时。 @ itsgoingdown的答案实际上是最好的(投票),因为它在克隆对象之前进行过滤。

Some of the posted solutions fail when tested, especially when the input is varied slightly. @itsgoingdown's answer actually works the best (up voted) because it filters before cloning the object.

无论如何,我认为我会将答案作为替代方案发布。它使用 JSON stringify和解析方法浅克隆对象。这消除了必须过滤for循环中的属性。理想情况下,人们还希望在更改属性名称之前检查名称冲突。这还没有在这里或任何解决方案中完成。

Regardless, I thought I'd post my answer as an alternative. It uses JSON stringify and parse methods to shallow clone the object. This eliminates having to filter the properties in the for loop. Ideally, one would also want to check for name collisions prior to changing a property name. That has not been done here or in any of the solutions.

选中显示代码然后运行代码片段以尝试。

// Same as JSON.stringify, but replaces whitespace in property names.

function stringify(obj, replacer, space) {
  var key, prop, copy = JSON.parse(JSON.stringify(obj));
  for (prop in copy) {
    key = prop.replace(/\W/g, '_');
    if (key != prop) {
      copy[key] = copy[prop];
      delete copy[prop];
    }
  }
  return JSON.stringify(copy, replacer, space);
}

// Same as JSON.stringify, but replaces whitespace in property names.

function stringify(obj, replacer, space) {
  var key, prop, copy = JSON.parse(JSON.stringify(obj));
  for (prop in copy) {
    key = prop.replace(/\W/g, '_');
    if (key != prop) {
      copy[key] = copy[prop];
      delete copy[prop];
    }
  }
  return JSON.stringify(copy, replacer, space);
}





var data = {
  "test 1": function(a) {
    return 1;
  },
  "test 2": [1, 2, 3, 4, 5],
  "test 3": "Hello World!",
  "Housing": 0,
  "Late Comers": 0,
  "Income cut": "12500",
  "Study Allowance": 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
};



document.getElementById('stdout').innerHTML = stringify(data, false, '  ');

<xmp id="stdout"></xmp>

这篇关于如何删除键和对象值之间的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆