Root nodes in JSON

I am tasked with determining the relationship between the two web applications. I decided to use JSON for this. How common is this in the root of a node in JSON?

Say we have a car. This is JSON with "Car" being the root of the node:

{"Car": { "Make":"Mustang", "YearBuilt":"1999"}} 

So, now let's say that I have a Tire object, and since we are standardizing the presence of root nodes, this should also have it.

 {"Tire": {"Make": "Brirdgestone", "Size":"15"}} 

Integrating a JSON bus object into the original Car object shows how bulky it is.

 {"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"}} ]}} 

Thus serialized in PHP, the first of the buses will be $object->Car->Tires[0]->Tire->Make . There is an additional layer of Tire because of the root node.

If Tire does not have a root root, the code can be much more subtle.

 {"Car": { "Make":"Mustang", "YearBuilt":"1999", "Tires": "[{ {"Make": "Bridgestone", "Size":"15"}}, {"Make": "Brirdgestone", "Size":"15"}}, {"Make": "Brirdgestone", "Size":"15"}}, {"Make": "Brirdgestone", "Size":"15"}}]}} 

There is less confusion in PHP because there is less redundancy: the first bus brand is called $object->Car->Tires[0]->Make

Is there anything bad without having a root root? I like having the root directory of the node, because it acts like a class name, but unnecessary levels change me a lot, and that will make moving more complicated.

+6
json javascript php
source share
2 answers

I would omit both root nodes, Tire and Car.

Keep in mind that the main use of JSON is to transfer objects over the network in a compact format. There is no other other use. You want to work with the data encoded by JSON, and adding the root nodes, you create empty container objects without real identification and purpose. When lowering the root nodes you get

 $car->tires[0]->make 

and in JS you get

 car.tires[0].make 

This is much clearer and the subject is much better. Remember, this is what you have to work with. Of course, you could use some kind of JSON-carper, which displays how objects should be serialized and which lead to the above objects, but it takes a lot of effort and is not worth it.

If you want to have the class name in JSON, just make this property, for example.

 { 'klass': 'Car', 'make': 'Mustang', 'year':1999 } 
+4
source share

@ Gordon makes good points. However, I like to use AOP with AJAX messages to intercept messages to and from the server. These interceptors add a timestamp to each message and messages sent from the server by a status flag (failure, exception, success, regardless ...). As you probably suggested, I use root JSON node for payload, status and timestamp.

The status flag sent from the server can be checked with one function (aspect) on the front side and all exceptions handled in one place. When a server crashes for any reason other than an exception, an aspect / tip allows you to pass data to the AJAX initiator.

0
source share

All Articles