JSON中的根节点 [英] Root Nodes in JSON
问题描述
我的任务是定义两个Web应用程序之间的通信.我决定为此使用JSON.在JSON中拥有根节点有多普遍?
I'm tasked with defining communication between two web apps. I've decided to use JSON for this. How common is it to have a root node in the JSON?
假设我们有一个汽车物体.这是带有"Car"作为根节点的JSON:
Let's say we have a car object. This is the JSON with "Car" being the root node:
{"Car": {
"Make":"Mustang",
"YearBuilt":"1999"}}
现在让我们说我有一个Tire对象,并且由于我们标准化了具有根节点,因此该节点也必须具有它.
So now let's say I have a Tire object and since we are standardizing on having root nodes, this one also has to have it.
{"Tire": {"Make": "Brirdgestone", "Size":"15"}}
将轮胎对象JSON集成到原始的Car对象中显示出它可能多么笨拙.
Integrating the tire object JSON into the original Car object shows how unwieldy it could be.
{"Car": { "Make":"Mustang",
"YearBuilt":"1999",
"Tires": "[{"Tire": {"Make": "Brirdgestone", "Size":"15"}},
{"Tire": {"Make": "Brirdgestone", "Size":"15"}},
{"Tire": {"Make": "Bridgestone", "Size":"15"}},
{"Tire": {"Make": "Brirdgestone", "Size":"15"}}
]}}
因此在PHP中进行了序列化,第一个Tire的品牌名称为$object->Car->Tires[0]->Tire->Make
.由于有根节点,所以那里有额外的Tyre级.
So serialized in PHP, the make of the first Tire would be $object->Car->Tires[0]->Tire->Make
. There's that extra Tire level there because of the root node.
如果Tire没有根节点,则代码可能会更苗条.
If Tire did not have the root node the code could be a lot slimmer.
{"Car": { "Make":"Mustang",
"YearBuilt":"1999",
"Tires": "[{ {"Make": "Bridgestone", "Size":"15"}},
{"Make": "Brirdgestone", "Size":"15"}},
{"Make": "Brirdgestone", "Size":"15"}},
{"Make": "Brirdgestone", "Size":"15"}}]}}
在PHP中,由于较少的冗余而减少了混乱:第一个轮胎的品牌由$object->Car->Tires[0]->Make
In PHP, there's less confusion because there's less redundancy: The make of the first tire is called by $object->Car->Tires[0]->Make
没有根节点是否有什么不好的地方?我喜欢拥有根节点,因为它的行为就像一个类名,但是不必要的级别会困扰我很多,并且会使遍历变得更加复杂.
Is there anything bad by not having a root node? I like having the root node because it acts likes a class name, but needless levels bug me a lot and will make traversing more complex.
推荐答案
我将省略两个根节点 Tire 和 Car .
I'd omit both root nodes, Tire and Car.
请记住,JSON的主要用途是以紧凑格式通过网络传输对象.除此之外,没有其他实际用途.您要使用JSON编码的数据,并通过添加根节点来创建没有实际标识和用途的空容器对象.省略根节点时,您会得到
Keep in mind that JSON's primary use is transfering objects over the network in a compact format. There is no real other usage beside this. You want to work with the data the JSON encodes and by adding the root nodes, you are creating empty container objects with no real identity and purpose. When omiting the root nodes, you get
$car->tires[0]->make
在JS中您将获得
car.tires[0].make
这更清楚,代表对象更好.请记住,这是您将要使用的.当然,您可以使用某种JSON映射器来映射对象应如何序列化并生成上面的对象,但这是很多额外的工作,不值得恕我直言.
This is much clearer and represents the object much better. Remember, this is what you will have to work with. Sure, you could use some sort of JSON mapper that maps how objects should be serialized and that result in the above objects, but that's a lot of extra effort and not worth it imho.
如果您想在JSON中使用类名,只需将其设为属性即可,例如
If you want to have the class name in the JSON, just make it a property, e.g.
{ 'klass': 'Car', 'make': 'Mustang', 'year':1999 }
这篇关于JSON中的根节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!