admin管理员组

文章数量:1335092

How to remove the null value in json string using jquery

var jsonstring=[null,null,{"rank":"23","credit":"10"},null,null,{"rank":"26","credit":"10"},null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,{"rank":"31","credit":"05"}]

How to remove the null value in json string using jquery

var jsonstring=[null,null,{"rank":"23","credit":"10"},null,null,{"rank":"26","credit":"10"},null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,{"rank":"31","credit":"05"}]

Share Improve this question edited Apr 23, 2015 at 10:40 vengatesh rkv asked Apr 23, 2015 at 10:00 vengatesh rkvvengatesh rkv 3272 silver badges16 bronze badges 4
  • unjson, remove null, json again? – sectus Commented Apr 23, 2015 at 10:02
  • 1 javascript solution to your question: stackoverflow./questions/281264/… – Pawan Commented Apr 23, 2015 at 10:02
  • You can try this on:- jsonstring=JSON.stringify(jsonstring).replace(/null/g,' '); jsonstring.replace(/ ,/g,'') – Tanul Commented Apr 23, 2015 at 10:29
  • Please give the example for my jsonstring – vengatesh rkv Commented Apr 23, 2015 at 10:45
Add a ment  | 

3 Answers 3

Reset to default 4
var arr = JSON.parse(json_string);
arr = arr.filter(function(n){ return n }); 
json_string = JSON.stringify(arr)

Use like this

(function filter(obj) {
    $.each(obj, function(key, value){
        if (value === "" || value === null){
            delete obj[key];
        } else if (Object.prototype.toString.call(value) === '[object Object]') {
            filter(value);
        } else if ($.isArray(value)) {
            $.each(value, function (k,v) { filter(v); });
        }
    });
})(sjonObj);

You should use array access notation please see the below code

    delete sjonObj[key];


 $.each(sjonObj, function(key, value){
 if (value === "" || value === null){
    delete sjonObj[key];
  }
 });

Thanks

本文标签: javascriptHow to remove the null value in json string using jqueryStack Overflow