JSON 未定义值类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13796751/
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-09-03 18:39:17  来源:igfitidea点击:

JSON undefined value type

json

提问by Steven Penny

I came across this JSON code. I noticed that it makes use of undefinedvalue. Where can I find more information about this value type?

我遇到了这个 JSON 代码。我注意到它利用了undefined价值。在哪里可以找到有关此值类型的更多信息?

  tracks:[
     (         {
        codec:"h264",
        language:undefined,
        id:1,
        bitrate:785236,
        content:"video"
     }         ),
     (         {
        codec:"aac",
        language:undefined,
        id:2,
        bitrate:75969,
        content:"audio"
     }         )
  ],

回答by Ringo

  • undefinedis not a valid json value, even though it is valid in javascript. From the official json standard(ECMA-404, Section 5):

    A JSON value can be an object, array, number, string, true, false, or null.

  • For json, use nullinstead of undefined: { "something": null }

  • undefined不是有效的 json 值,即使它在 javascript 中有效。来自官方 json 标准(ECMA-404,第 5 节):

    JSON 值可以是对象、数组、数字、字符串、true、false 或 null。

  • 对于 json,使用null而不是undefined{ "something": null }

回答by Murali Prasanth

undefinedis a special type where it simply indicates that the variable languageis not initialized or may be it's not yet defined.

undefined是一种特殊类型,它仅指示变量language未初始化或尚未定义。

nullin javascriptsimply indicates absence of a value, and it can also be used to indicate “no value” for numbers and strings as well as objects.The undefinedvalue also represents absence of value, it is the value of the variables that have not been initialized and the value when you get from objectproperty or arrayelement that doesn't exist undefinedis a predefined global variable that is initialized to undefined value.

nullinjavascript简单地表示没有值,也可以用来表示数字和字符串以及对象的“无值”。undefined值也表示没有值,它是未初始化的变量的值,从不存在的object属性或array元素 获取的值undefined是预定义的全局变量,该变量被初始化为未定义的值。

nulland undefineddoesn't have any properties or methods.In fact, using .or []to access property or method of these values causes a TypeError.

null并且undefined没有任何属性或方法。事实上,使用.[]访问这些值的属性或方法会导致TypeError.