V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
awesomelei
V2EX  ›  程序员

如何表达 null 在 json 中

  •  
  •   awesomelei · 2018-12-26 09:28:19 +08:00 · 2980 次点击
    这是一个创建于 1947 天前的主题,其中的信息可能已经有所发展或是发生改变。

    https://www.json.org/ 官网右侧图片中将 true, false, null 都加上了双引号, 是否会造成误导? 我个人还是倾向于{"foo": null}, 不知大家如何抉择{"foo": []}, {"foo": ""}, {"foo": "null"}, {"foo": 0} 是否是根据数据类型来选择各种空值, 还是直接表示为 null, 但总之, 我个人感觉官网中的右侧图片表达的"null"写法是最可能陷入麻烦的, 也就是{"foo": "null"}

    引用下爆栈网的提问: https://stackoverflow.com/questions/21120999/representing-null-in-json 大家发表下自己喜好吧

    Terry05
        1
    Terry05  
       2018-12-26 09:30:19 +08:00
    就是用 null 不带引号

    { "a": null }
    chairuosen
        2
    chairuosen  
       2018-12-26 09:30:58 +08:00
    > JSON.stringify({a:null})
    > "{"a":null}"
    dengtongcai
        3
    dengtongcai  
       2018-12-26 09:38:15 +08:00
    "null"代表字符串 null
    awesomelei
        4
    awesomelei  
    OP
       2018-12-26 09:39:42 +08:00
    行, 个人感觉还是官网那张图片可能存在歧义, 此贴终结
    @Terry05
    @chairuosen
    @dengtongcai
    canbingzt
        5
    canbingzt  
       2018-12-26 10:24:16 +08:00
    个人理解
    官网右边的 object、array、string、number 代表类型,没加""
    而 "true"、"false"、"null",是具体的值,加了""
    zxcvsh
        6
    zxcvsh  
       2018-12-26 10:28:31 +08:00 via iPhone
    和后台商量好就行
    bumz
        7
    bumz  
       2018-12-26 10:52:06 +08:00
    没有歧义
    它的文法中用到的默认是语法变量,比如 object, array, string, number
    终极符号都加上引号,"true", "false", "null"
    实际描述的语言 (json) 是不包括那个引号的。
    dacapoday
        8
    dacapoday  
       2018-12-26 11:11:16 +08:00
    官网那个是 PEG,用来描述语言的语言,引号是表示语言中的文本,以便和其他解析符号区别。
    awesomelei
        9
    awesomelei  
    OP
       2018-12-26 14:51:13 +08:00
    @canbingzt
    @bumz
    @dacapoday
    感谢大佬们解答, 明白了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1964 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 16:20 · PVG 00:20 · LAX 09:20 · JFK 12:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.