Javascript JSON 是否应包含空值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11003424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:58:39  来源:igfitidea点击:

Should JSON include null values

javascriptjsonnullundefined

提问by jjathman

I'm creating an API that returns results as JSON. Is there a current best practice for whether we should include keys in the result when the value is null? For example:

我正在创建一个以 JSON 形式返回结果的 API。当值为空时,是否应该在结果中包含键,是否有当前的最佳实践?例如:

{
    "title":"Foo Bar",
    "author":"Joe Blow",
    "isbn":null
}

or

或者

{
    "title":"Foo Bar",
    "author":"Joe Blow"
}

Since the second is smaller I am leaning towards this style, but I'm not sure if there is a preferred style or not. From a client perspective it seems like both styles would be functionally equivalent. Any pros or cons to each?

由于第二个较小,我倾向于这种风格,但我不确定是否有首选风格。从客户的角度来看,这两种风格似乎在功能上是等效的。各有优缺点吗?

采纳答案by Niet the Dark Absol

The second will save a small amount on bandwidth, but if that were a concern you would also use indexed arrays instead of filling the JSON with keys. Clearly, ["Foo Bar","Joe Blow"]is much shorter than what you have now.

第二个将节省少量带宽,但如果这是一个问题,您还可以使用索引数组而不是用键填充 JSON。显然,["Foo Bar","Joe Blow"]比你现在拥有的要短得多。

In terms of usability, I don't think it makes any difference. In both cases, if(json.isbn)will skip to the else. There is usually no need to distinguish between null(no value) and undefined(no given value).

在可用性方面,我认为它没有任何区别。在这两种情况下,if(json.isbn)都会跳到else. 通常不需要区分null(无值)和undefined(无给定值)。

回答by rushkeldon

I am a fan of always including null explicitly as that carries meaning. While omitting a property leaves ambiguity.

我喜欢总是明确包含 null ,因为它具有意义。省略属性会导致歧义。

As long as your protocol with the server is agreed upon any of the above can work, but if you pass nulls from the server I believe that makes your APIs more flexible later.

只要您与服务器的协议达成一致,上述任何一项都可以工作,但如果您从服务器传递空值,我相信这会使您的 API 以后更加灵活。

Should also mention that javascript's hasOwnProperty function gives you further insight.

还应该提到 javascript 的 hasOwnProperty 函数可以让您进一步了解。

/* if true object DOES contain the property with *some* value */
if( objectFromJSON.hasOwnProperty( "propertyName" ) )

/* if true object DOES contain the property and it has been set to null */
if( jsonObject.propertyName === null )

/* if true object either DOES NOT contain the property
   OR
   object DOES contain the property and it has been set to undefined */
if( jsonObject.propertyName === undefined )

回答by Brad

In JavaScript, nullmeans something very different than undefined.

在 JavaScript 中,这null意味着与undefined.

Your JSON output should reflect what is used and needed by your application in the specific context of using the JSON data.

您的 JSON 输出应反映您的应用程序在使用 JSON 数据的特定上下文中使用和需要的内容。

回答by Paul

You should definitely include it if there is any need to distinguish between nulland undefinedsince those have two different meanings in Javascript. You can think of nullas meaning the property is unknown or meaningless, and undefinedas meaning the property doesn't exist.

如果需要区分nullundefined因为它们在 Javascript 中具有两种不同的含义,则绝对应该包括它。您可以将其null视为该属性未知或无意义的意思,以及undefined该属性不存在的意思。

On the other hand, if there is no need for anyone to make that distinction then go ahead and leave it out.

另一方面,如果没有必要让任何人做出这种区分,那么继续并忽略它。

回答by Alexey Kachalov

I think it is no difference when you use the JSON as a data behind user's experience.

我认为当您使用 JSON 作为用户体验背后的数据时没有区别。

The difference appears in JSON-config files, when a user should edit something by hand. When you use the first example you give the user some hint about the config.

当用户应该手动编辑某些内容时,差异出现在 JSON-config 文件中。当您使用第一个示例时,您会给用户一些有关配置的提示。