将空值替换为JSON OBJECT中的空值 [英] Replace null values to empty values in a JSON OBJECT

查看:99
本文介绍了将空值替换为JSON OBJECT中的空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有一个由ajax请求提供的JSON对象。

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

json中的一些值显示为 null ,但我想要空字符串而不是

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

我的代码示例:

$.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');

任何想法?

推荐答案

你的功能应该是这样的:

Your function should be like this:

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

检查 null undefined 值并返回一个空字符串

Checks null and undefined values and returns an empty string

这篇关于将空值替换为JSON OBJECT中的空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆