2015-02-11 41 views
-2

JSON對象如下所示。我想刪除NULL值的所有鍵。另外,如果刪除對之後,如果條目是空白的,那麼也應該刪除。我試圖尋找執行所需任務的腳本,但是我沒有成功。請記住,它是一個NULL字符串,而不是一個對象。我需要實現這個是sails.js,所以Javascript是最受歡迎的語言。如何從使用Javascript的sails.js中的JSON對象中刪除「NULL」字符串?

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

} }

+1

你從哪裏得到這個JSON? – Marty 2015-02-11 05:00:52

+0

它是更大項目的一部分。但是現在我需要刪除NULL字符串,並且我被卡住了。 – 2015-02-11 05:09:50

+0

你可以改變任何生成的JSON嗎?因爲這將是一個更好的主意。 – Marty 2015-02-11 05:12:11

回答

0

我同意,這是更好地固定來源,但即使正確格式化的來源將仍然具有空值(只是沒有字符串),並且由於你的問題解決消除這些以及我只會使用lodash或下劃線彈出不良信息。這是一個例子。你應該玩弄它,讓它恰到好處。帆包括lodash作爲依賴,否則練習的風帆外面只要確保包括lodash庫,如果neccessary 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); 

我可以理解,有時你從源不良信息,請參閱文檔。在我這個時代,我必須修補大量遺留系統。

+0

我找到了一個方法來做到這一點,不過謝謝! – 2015-02-24 09:48:30

+0

然後你應該發佈你的方法作爲答案,以便其他人可以受益,因爲你有。 – Meeker 2015-02-24 14:43:44

0
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 
    } 
相關問題