【问题标题】:How to remove "NULL" strings from a JSON object in sails.js using Javascript?如何使用Javascript从sails.js中的JSON对象中删除“NULL”字符串?
【发布时间】:2015-04-11 09:12:37
【问题描述】:

JSON 对象如下图所示。我想删除所有值为 NULL 的键。此外,如果在删除该对后,如果条目为空白,则也应删除该条目。我曾尝试寻找执行所需任务的脚本,但我没有成功。请记住,它是一个 NULL 字符串,而不是一个对象。我需要实现的是sails.js,所以Javascript是最首选的语言。

var JSONobj ={  "regex_attributes": {
"matching attributes": {
  "transaction_amount": {
    "matching_position": "1"
  },
  "total_amount_due": {
    "matching_position": "NULL"
  },
  "transaction_date": {
    "matching_position": "NULL",
    "date_format": "NULL"
  },
  "current_credit": {
    "matching_position": "NULL"
  },
  "available_credit": {
    "matching_position": "NULL"
  },
  "date_posted": {
    "matching_position": "NULL",
    "date_format": "NULL"
  },
  "merchant": {
    "matching_position": "NULL"
  },
  "bill_date": {
    "matching_position": "NULL",
    "date_format": "NULL"
  },
  "bill_due_date": {
    "matching_position": "NULL",
    "date_format": "NULL"
  },
  "bill_amount": {
    "matching_position": "NULL"
  },
  "account_number_ending": {
    "matching_position": "2"
  },
  "balance_date": {
    "matching_position": "NULL",
    "date_format": "NULL"
  },
  "available_balance": {
    "matching_position": "NULL"
  }
},
"additional_processing": [     //Array
  {
    "sms_sender_name": "NULL",
    "type_of_account_to_find": "NULL",
    "attribute": "description",
    "operation": "pre_add",
    "pre_add_1": "Credit"
  }
],
"account_details": {
  "credit_or_debit": "CREDIT",
  "type_of_transaction": "INT",
  "type_of_account": "SAVINGS",
  "type_of_action": "TRANSACTION",
  "bills_account_type": "NULL"
}

} }

【问题讨论】:

  • 你从哪里得到这个 JSON?
  • 这是一个更大项目的一部分。但现在我需要删除 NULL 字符串,我被困在了那里。
  • 你能改变任何生成 JSON 的东西吗?因为那会是一个更好的主意。
  • 好的,我会试试这个,而不是这个。但是,如果您有解决方案,请告诉我。谢谢。
  • 最好尽可能在上游尝试解决问题 :-)

标签: javascript json sails.js


【解决方案1】:
for(var key in JSONObj.regex_attributes.matching_attributes){
    for (var key2 in JSONObj.regex_attributes.matching_attributes[key])
    {
      var val = JSONObj.regex_attributes.matching_attributes[key][key2]
      if (val == 'NULL')
          delete JSONObj.regex_attributes.matching_attributes[key][key2]
    }
    }


  for (var key in JSONObj.regex_attributes.matching_attributes){
    var k=Object.keys(JSONObj.regex_attributes.matching_attributes[key]).length
    if (k==0){
      delete JSONObj.regex_attributes.matching_attributes[key]
    }
  }

  for (var key in JSONObj.regex_attributes){
    var k=Object.keys(JSONObj.regex_attributes[key]).length
    if (k==0){
      delete JSONObj.regex_attributes[key]
    }
  }

  var count2=0
  for (var key in JSONObj.regex_attributes.additional_processing[0]) {

      var val = JSONObj.regex_attributes.additional_processing[0][key];
      if (val == 'NULL'){
        delete JSONObj.regex_attributes.additional_processing[0][key]
        count2=count2+1
      }
  }

  if(count2==6){
      delete JSONObj.regex_attributes.additional_processing
  }

  var count3=0
  for (var key in JSONObj.regex_attributes.account_details) {

      var val = JSONObj.regex_attributes.account_details[key];
      if (val == 'NULL'){
        delete JSONObj.regex_attributes.account_details[key]
        count3=count3+1
      }

  }
  if (count3==5){
    delete JSONObj.regex_attributes.account_details
  }

【讨论】:

    【解决方案2】:

    我同意最好修复源,但是即使格式正确,源仍然会有 NULL(只是不是字符串),因为你的问题也解决了删除这些问题,所以我只会使用 lodash 或下划线来弹出出不良信息。这是一个例子。你应该玩弄它以使其恰到好处。 Sails 包含 lodash 作为依赖项,否则要在 Sails 之外进行练习,只需确保包含 lodash 库并在必要时参考文档https://lodash.com/docs

    _.mixin({
      cleanObject: function(o) {
        _.each(o, function(v, k) {
          if(k === 'NULL') {
            delete o[k];
          }
          else if(!v) {
            delete o[k];
          }
        });
        return o;
      }
    });
    
    var cleanedUp = _.cleanObject(JSONobj);
    

    我可以理解,有时您会从源头获得错误信息。在我的时代,我不得不修补大量遗留系统。

    【讨论】:

    • 那么您应该发布您的方法作为答案,以便其他人可以像您一样受益。
    猜你喜欢
    • 1970-01-01
    • 2021-10-14
    • 2010-10-13
    • 2018-03-08
    • 1970-01-01
    • 1970-01-01
    • 2012-10-29
    • 1970-01-01
    相关资源
    最近更新 更多