JSON 根元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10426924/
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
JSON root element
提问by cacert
Does JSON require a root element as in XML case. As far as I know this is a valid JSON string.
JSON 是否需要像 XML 一样的根元素。据我所知,这是一个有效的 JSON 字符串。
{
"email":[
{
"type":"home",
"name":"[email protected]"
},
{
"type":"work",
"name":"[email protected]"
}
]
}
I need to convert JSON to XML an vice versa. However although the above is valid JSON when I convert it to XML it is not valid? Am I missing something or this is normal?
我需要将 JSON 转换为 XML,反之亦然。但是,虽然当我将其转换为 XML 时,上面的 JSON 是有效的,但它是无效的?我错过了什么还是这是正常的?
回答by Michael Kay
The outermost level of a JSON document is either an "object" (curly braces) or an "array" (square brackets).
JSON 文档的最外层是“对象”(花括号)或“数组”(方括号)。
Any software that converts JSON to XML has to reconcile the fact that they are different data models with different rules. Different conversion tools handle these differences in different ways.
任何将 JSON 转换为 XML 的软件都必须调和这样一个事实,即它们是具有不同规则的不同数据模型。不同的转换工具以不同的方式处理这些差异。
回答by Jpsy
According to the modified Backus-Naur-Form on the right side pane of http://json.org/the root element of a JSON data structure can be anyof these seven types/values:
根据http://json.org/右侧窗格中修改后的 Backus-Naur-Form,JSON 数据结构的根元素可以是以下七种类型/值中的任何一种:
Object
Array
String
Number
true
false
null
Examples
例子
So all of the following examples are valid JSON root elements:
因此,以下所有示例都是有效的 JSON 根元素:
{
"name": "Jpsy",
"age": 99
}
[ 1, 2, "three", 4, 5 ]
"abcdefg"
123.45
true
false
null
回答by Seb Cesbron
This is normal, json and xml don't have the same rules. You can transfrom your root brackets "{" and "}" into a root element to be sure to don't have conversion problems
这是正常的,json 和 xml 没有相同的规则。您可以将根括号“{”和“}”转换为根元素,以确保没有转换问题

