当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Replace null values to empty values in a JSON OBJECT

问题描述:

Hi I've got a JSON object provided by an ajax request.

Some of the values inside the json appears as null, but I want an empty String instead

My sample of code :

$.post("/profil_process/wall/preview-post.php",param, function (data){

// this does not work

JSON.stringify(data, function(key, value) { return value === "" ? "" : value });

$('#previewWall').html(getPostWall(data.type,data.titre,data.url,data.description,data.media,data.photo_auteur,data.nom_auteur,data.url_auteur,data.date_publication)).fadeIn();

$(".bouton-vertM").show();

$("#wLoader").hide();

},'json');

Any ideas?

网友答案:

Your function should be like this:

function (key, value) {
    return (value == null) ? "" : value
}

Checks null and undefined values and returns an empty string

网友答案:

If you can replace null-s with empty strings on serialized string, do something like this:

JSON.stringify(data).replace(/null/i, "\"\""); 
网友答案:

Here's how you should be doing it, replacing the objects values with empty strings, not stringifying it

$.post("/profil_process/wall/preview-post.php",param, function (data){

    (function removeNull(o) {
        for(var key in o) {
            if( null === o[key] ) o[key] = '';
            if ( typeof o[key] === 'object' ) removeNull(o[key]);
        }
     })(data);

     $('#previewWall').html(
          getPostWall(
              data.type,
              data.titre,data.url,
              data.description,
              data.media,
              data.photo_auteur,
              data.nom_auteur,
              data.url_auteur,
              data.date_publication
          )  // ^^^ why not just pass the entire object ?
    ).fadeIn();

    $(".bouton-vertM").show();
    $("#wLoader").hide();

},'json');
网友答案:

Hope this help

var List = [];


$.post("/profil_process/wall/preview-post.php",param, function (data){

jQuery.each(data, function (key, value) {
                List.push({
                    ReportId : value.ReportId,
                    ReportType: CheckNullReturnBlank(value.ReportType),
                    ReportName: CheckNullReturnBlank(value.ReportName),
                    Description : CheckNullReturnBlank(value.Description)
                })
            });   },'json');



function CheckNullReturnBlank(item) {
    return item = (item == null) ? '' : item;
}
网友答案:
function returnblank(item){
  if(item == null){
    return "";
  }else{
    return item;
  }
}
网友答案:

You can replace null values to empty by below code in java-script

var remove_empty = function ( target ) {
  Object.keys( target ).map( function ( key ) {
    if ( target[ key ] instanceof Object ) {
      if ( ! Object.keys( target[ key ] ).length && typeof target[ key ].getMonth !== 'function') {
        target[ key ] = "";
      }
      else {
        remove_empty( target[ key ] );
      }
    }
    else if ( target[ key ] === null ) {
       target[ key ] = "";
    }
  } );
  return target;
};

you can read more about Object.keys

分享给朋友:
您可能感兴趣的文章:
随机阅读: