列表和布尔值之间的JSON数据结构是否有效? [英] Is it valid for JSON data structure to vary between a list and a boolean

查看:74
本文介绍了列表和布尔值之间的JSON数据结构是否有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jstree的json数据结构在 https://github.com/vakata/jstree,这是一个示例

[ { "text" : "Root node", "children" : [ "Child node 1", "Child node 2" ] } ]

值得注意的是

children键可用于将孩子添加到分支中,它应该 是一个数组

但是稍后在使用AJAX和延迟加载节点填充树部分中,该示例显示将set child设置为false来指示何时未处理child

[{
  "id":1,"text":"Root node","children":[
    {"id":2,"text":"Child node 1","children":true},
    {"id":3,"text":"Child node 2"}
  ]
}]

因此,在这里我们看到子级既用作数组又用作 boolean

我以 jstree 为例,因为这是我遇到的问题,但是我的问题实际上是一个通用的json问题.我的问题是,对于JSON中的相同元素,它是否为两种不同类型(数组和布尔值)是否有效JSON

解决方案

从结构上讲,它们都是有效的JSON数据包.没关系,因为JSON比XML(使用XSD或DTD)严格一些.按照: https://www.w3schools.com/js/js_json_objects.asp

JSON objects are surrounded by curly braces {}.
JSON objects are written in key/value pairs.
Keys must be strings, and values must be a valid JSON data type (string, number, object, array, boolean or null).
Keys and values are separated by a colon.
Each key/value pair is separated by a comma.

已经说过,如果允许发送方发送此类JSON,则仅需说明的是,服务器端在接收到此类不同的数据包时将必须处理此差异.这是一个看起来很糟糕的合同,因此服务器可能需要做一些额外的工作来管理它.服务器端处理此类传入的JSON数据包可能会变得很棘手.

请参阅: https://jsonlint.com/

在此答案中了解有关JSON的更多信息: https://stackoverflow.com/a/4862511/945214

The json data structure for jstree is define in https://github.com/vakata/jstree, here is an example

[ { "text" : "Root node", "children" : [ "Child node 1", "Child node 2" ] } ]

Notably it says

The children key can be used to add children to the branch, it should be an array

However later on in section Populating the tree using AJAX and lazy loading nodes it shows to use set children to false to indicate when a child has not be processed

[{
  "id":1,"text":"Root node","children":[
    {"id":2,"text":"Child node 1","children":true},
    {"id":3,"text":"Child node 2"}
  ]
}]

So here we see children used as both as an array and as a boolean

I am using jstree as an example because this is where I encountered the issue, but my question is really a general json question. My question is this, is it valid JSON for the same element in json to be two different types (an array and a boolean)

解决方案

Structure wise, both are valid JSON packets. This is okay, as JSON is somewhat less stricter than XML(with a XSD or a DTD). As per: https://www.w3schools.com/js/js_json_objects.asp,

JSON objects are surrounded by curly braces {}.
JSON objects are written in key/value pairs.
Keys must be strings, and values must be a valid JSON data type (string, number, object, array, boolean or null).
Keys and values are separated by a colon.
Each key/value pair is separated by a comma.

Having said that, if the sender is allowed to send such JSONs, only caveat is that server side will have to handle this discrepancy upon receiving such different packets. This is a bad-looking-contract, and hence server might need to do extra work to manage it. Server side handling of such incoming JSON packets can become tricky.

See: How do I create JSON data structure when element can be different types in for use by

You could validate whether a JSON is okay or not at https://jsonlint.com/

See more about JSON in this answer: https://stackoverflow.com/a/4862511/945214

这篇关于列表和布尔值之间的JSON数据结构是否有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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