简体   繁体   English

"用 JSON 中的引号替换 null"

[英]Replace null with quotes in JSON

This topic is addressed on this site for PHP and Rails but I don't see anything for standard JavaScript.该主题在此站点上针对 PHP 和 Rails 进行了讨论,但我没有看到任何标准 JavaScript 的内容。

If my JSON object has a null value in a pair it looks like this id:null<\/strong> but I need it to look like this id:""<\/strong>如果我的 JSON 对象在一对中有一个 null 值,它看起来像这个id:null<\/strong>但我需要它看起来像这个id:""<\/strong>

Since JSON doesn't support single quotes I'm unsure how to do this.由于 JSON 不支持单引号,我不确定如何执行此操作。

Current Result<\/strong>当前结果<\/strong>

{"id":"e6168d55-1974-e411-80e0-005056971214","label":"List","parentId":null}

JSON.stringify accepts a replacer callback that allows you to substitute values with other values. JSON.stringify接受一个替换回调,允许你用其他值替换值。 The replacer callback runs for every key-value pair processed in the input, and it replaces the supplied value with it return value.替换回调针对输入中处理的每个键值对运行,并用返回值替换提供的值。

Simply have your replacer callback look for any null values and replace them with the empty string:只需让您的替换器回调查找任何null值并将它们替换为空字符串:

var myObj = {
               "id":"e6168d55-1974-e411-80e0-005056971214",
               "label":"List",
               "parentId":null
             };

JSON.stringify(myObj, function(key, value) {
    // if value is null, return "" as a replacement
    if(value === null) {
        return "";
    }

    // otherwise, leave the value unchanged
    return value;
});

If you don't have an object, but only have JSON as input, you can build an object using var myObj = JSON.parse(jsonInput);如果您没有对象,而只有 JSON 作为输入,则可以使用var myObj = JSON.parse(jsonInput);构建对象var myObj = JSON.parse(jsonInput);

Not sure why you're mentioning single quotes, but in json it would never look like this:不知道你为什么提到单引号,但在 json 中它永远不会是这样的:

id:null

Perhaps you meant this?也许你是这个意思?

{
  "id": null
}

Regardless, if you want to encode "" instead of null , you provide an empty string, because "" is simply how you encode an empty string.无论如何,如果你想编码""而不是null ,你提供一个空字符串,因为""只是你编码空字符串的方式。 So go through your null s and replace them with empty strings.因此,检查您的null并用空字符串替换它们。

I do not believe there is a quick and easy way to map NULL to "".我不相信有一种快速简便的方法可以将 NULL 映射到“”。 If you are not working with a very deep JSON structure then you can iterate through the structure and change it from NULL to "".如果您没有使用非常深的 JSON 结构,那么您可以遍历该结构并将其从 NULL 更改为“”。

这解决了我的问题

  myJSON = myJSON.replace(/null/i, "\"\"");

Try this.尝试这个。

var response = yourResponseText.replace(/null/g, '')
var json = JSON.parse(response);

Replace null in your response text with '' .将响应文本中的null替换为'' After replacing, parse response string to JSON .替换后,将response字符串解析为JSON

Try this试试这个

const obj = {
    "id": "e6168d55-1974-e411-80e0-005056971214",
    "label": "List",
    "parentId": null
}

const jsonObj = JSON.stringify(obj,
    (key, value) => (value === null) ? '' : value
);

console.log(jsonObj);
// return -> {"id":"e6168d55-1974-e411-80e0-005056971214","label":"List","parentId":""}
{"id":"e6168d55-1974-e411-80e0-005056971214","label":"List","parentId": "parentId" ? "parentId" : ''}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM