Hi I've got a JSON object provided by an ajax request.
嗨,我有一个由ajax请求提供的JSON对象。
Some of the values inside the json appears as null
, but I want an empty String
instead
json中的一些值显示为null,但我想要一个空String
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?
有任何想法吗?
6 个解决方案
#1
16
Your function should be like this:
你的功能应该是这样的:
function (key, value) {
return (value == null) ? "" : value
}
Checks null
and undefined
values and returns an empty string
检查null和undefined值并返回一个空字符串
#2
5
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');
#3
4
If you can replace null-s with empty strings on serialized string, do something like this:
如果可以在序列化字符串上用空字符串替换null-s,请执行以下操作:
JSON.stringify(data).replace(/null/i, "\"\"");
#4
-1
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;
}
#5
-1
function returnblank(item){
if(item == null){
return "";
}else{
return item;
}
}
#6
-1
You can replace null values to empty by below code in java-script
您可以通过java脚本中的以下代码将空值替换为空
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
您可以阅读有关Object.keys的更多信息
#1
16
Your function should be like this:
你的功能应该是这样的:
function (key, value) {
return (value == null) ? "" : value
}
Checks null
and undefined
values and returns an empty string
检查null和undefined值并返回一个空字符串
#2
5
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');
#3
4
If you can replace null-s with empty strings on serialized string, do something like this:
如果可以在序列化字符串上用空字符串替换null-s,请执行以下操作:
JSON.stringify(data).replace(/null/i, "\"\"");
#4
-1
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;
}
#5
-1
function returnblank(item){
if(item == null){
return "";
}else{
return item;
}
}
#6
-1
You can replace null values to empty by below code in java-script
您可以通过java脚本中的以下代码将空值替换为空
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
您可以阅读有关Object.keys的更多信息