Javascript 理解 JSON 结构 - 属性和值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8383841/
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
Understanding JSON structure - attributes and values
提问by Mike Sav
Something is bothering me. I've used JSON in a few of my apps/websites, we all love it! However something entered my head today which I've never thought about.. take a look at the following example (this is taken from http://json.org/example.html):
有件事困扰着我。我在我的一些应用程序/网站中使用了 JSON,我们都喜欢它!然而,今天有一些事情进入了我的脑海,我从未想过……看看下面的例子(这取自http://json.org/example.html):
{"widget": {
"debug": "on",
"window": {
"title": "Sample Konfabulator Widget",
"name": "main_window",
"width": 500,
"height": 500
},
"image": {
"src": "Images/Sun.png",
"name": "sun1",
"hOffset": 250,
"vOffset": 250,
"alignment": "center"
},
"text": {
"data": "Click Here",
"size": 36,
"style": "bold",
"name": "text1",
"hOffset": 250,
"vOffset": 100,
"alignment": "center",
"onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
}
}}
now if this was expressed as XML: we would have the following:
现在,如果这表示为 XML:我们将有以下内容:
<widget>
<debug>on</debug>
<window title="Sample Konfabulator Widget">
<name>main_window</name>
<width>500</width>
<height>500</height>
</window>
<image src="Images/Sun.png" name="sun1">
<hOffset>250</hOffset>
<vOffset>250</vOffset>
<alignment>center</alignment>
</image>
<text data="Click Here" size="36" style="bold">
<name>text1</name>
<hOffset>250</hOffset>
<vOffset>100</vOffset>
<alignment>center</alignment>
<onMouseUp>
sun1.opacity = (sun1.opacity / 100) * 90;
</onMouseUp>
</text>
</widget>
When looking at JSON to XML transformation I'm wondering if is there is any way that you can distinguish if a pair ("key":"value") are a attribute of a tag. For example, in our JSON above
在查看 JSON 到 XML 的转换时,我想知道是否有任何方法可以区分一对 ("key":"value") 是否是标签的属性。例如,在我们上面的 JSON 中
"window": {
"title": "Sample Konfabulator Widget",
"name": "main_window",
title is represented as an attribute of the window tag whilst name is a tag in it's own right, however this isn't expressed anywhere (that I can see).
title 表示为 window 标签的一个属性,而 name 本身就是一个标签,但是这并没有在任何地方表达(我可以看到)。
<window title="Sample Konfabulator Widget">
<name>main_window</name>
Why isn't this displayed as:
为什么不显示为:
<window>
<title>Sample Konfabulator Widget</title>
<name>main_window</name>
Is their a way I can specify if a pair are an attribute of the parent tag or a child tag in their own right? Sorry if my wording is bad or if I'm not explaining myself well.
他们是我可以指定一对是父标签的属性还是子标签本身的一种方式?对不起,如果我的措辞不好或者我没有很好地解释自己。
回答by Demis Palma ツ
Some people tend to use a regular property named "@attributes", giving it the special meaning of "attributes container", but that name does not have any special meaning for the JSON specifications themselves.
有些人倾向于使用名为“@attributes”的常规属性,赋予它“属性容器”的特殊含义,但该名称对于 JSON 规范本身没有任何特殊含义。
To answer to your question, there is no way to distinguish between pairs "key":"value" and attributes, simply because json does not support attributes.
要回答您的问题,无法区分“键”:“值”和属性对,仅仅是因为 json 不支持属性。
This kind of question usually comes from people who forget that the M in XML stands for "markup", which is a way of taking plain text and adding markup to create structured text. I guess that the misunderstanding cames from promoting JSON as a fat-free alternative to XML, while actually XML is a markup language, and JSON is a data interchange format. No one is really an alternative to the other.
这种问题通常来自忘记了 XML 中的 M 代表“标记”的人,这是一种获取纯文本并添加标记以创建结构化文本的方式。我猜误解来自于宣传 JSON 作为XML 的无脂肪替代品,而实际上 XML 是一种标记语言,而 JSON 是一种数据交换格式。没有人真的可以替代另一个。
The mixed content is one of the contexts where XML shows its power.
混合内容是 XML 显示其力量的上下文之一。
<p>What a <b>mess</b> we're in.</p>
Trying to express that as JSON, you'll realize that it wasn't designed for the that job.
尝试将其表示为 JSON,您会意识到它不是为该工作而设计的。
On the other hand, XML may be quite useful as data interchanger, but that's not what it was designed for or where it's main strengths are.
另一方面,XML 作为数据交换器可能非常有用,但这不是它的设计目的,也不是它的主要优势所在。
回答by Krizz
It depends on your converter.
这取决于您的转换器。
Json does not match XML 1-1.
Json 与 XML 1-1 不匹配。
Simply put, json is just a way to represent dictionaries and arrays mutally nested.
简单地说,json 只是一种表示字典和数组相互嵌套的方式。
回答by Bas Slagter
Some converters are using some default keywords like 'title' and 'src' that are being tranformed as an attribute instead of a field. Some converters do not.
一些转换器使用一些默认关键字,如“title”和“src”,它们被转换为属性而不是字段。有些转换器没有。
回答by Gijs
Not within JSON. I'm assuming you used some kind of automatic way to convert to XML from JSON; depending on the program that's doing the transformation there may be annotations you can use. See also eg. http://en.wikipedia.org/wiki/JSONwhich gives two XML examples for the same JSON.
不在 JSON 中。我假设您使用某种自动方式从 JSON 转换为 XML;根据进行转换的程序,您可能会使用注释。另见例如。http://en.wikipedia.org/wiki/JSON为同一个 JSON 提供了两个 XML 示例。
There is no formal specification (that I know of) of how you're "supposed" to map JSON to XML. Both are formalized, structured ways to represent data, but leave the concrete parts of how you represent the data up to you. So it depends how you want to represent the data, and what your application can or cannot deal with.
没有正式的规范(据我所知)您“应该”如何将 JSON 映射到 XML。两者都是表示数据的形式化、结构化的方式,但让您如何表示数据的具体部分由您决定。因此,这取决于您希望如何表示数据,以及您的应用程序可以或不能处理什么。
回答by Garry
I just read this exact example on json website and thought "how on earth is title an attribute there's nothing to signify it should. I'd presume the example itself isn't wrong - but I don't know why anyone hasn't offered a simple answer?
我刚刚在 json 网站上阅读了这个确切的例子,并认为“标题到底是一个属性,没有什么可以表示它应该。我认为这个例子本身没有错 - 但我不知道为什么有人没有提供一个简单的答案?
回答by Jiminion
I think this is (somewhat) handled in that 'title' and 'description' (and 'default') are considered keywords in JSON. JSON doesn't really handle attributes that way. I think the only way to do it would be to add another encapsulating object just to separate 'title' from the other entries. I think JSON's effort to make 'title' and 'description' special is an effort to deal with this. (Not a wonderful answer, I know....)
我认为这是(在某种程度上)处理的,因为“标题”和“描述”(和“默认”)被认为是 JSON 中的关键字。JSON 并没有真正以这种方式处理属性。我认为唯一的方法是添加另一个封装对象,以将“标题”与其他条目分开。我认为 JSON 努力使“标题”和“描述”变得特别是为了解决这个问题。(不是一个很好的答案,我知道......)
https://spacetelescope.github.io/understanding-json-schema/reference/generic.html
https://spacetelescope.github.io/understanding-json-schema/reference/generic.html
回答by Ranadeep Das
The object "title" in "widget/window" will be generated into a new element. It will never get in to a attribute of another element. Very sure about this.
“widget/window”中的对象“title”将被生成为一个新元素。它永远不会进入另一个元素的属性。非常确定这一点。