为什么以及何时需要展平JSON对象? [英] Why and when do we need to flatten JSON objects?

查看:173
本文介绍了为什么以及何时需要展平JSON对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我感到很惊讶,之前没有人问过这个问题。

I am surprised that no one on st asked this question before.

查看Json对象文档和快速谷歌搜索并没有产生令人满意的结果。

Looking through Json object documentation and a quick google search did not yield satisfactory results.

它有什么优势?它是如何工作的?

What's the advantage of it? How it works?

编辑:为了清楚起见,请看一下这个展平/不展平的例子。

To make it clear, take a look at this flatten/un-flatten example.

压扁/取消嵌套嵌套JSON对象的最快方法

谢谢。

推荐答案

在很多情况下,您可以获得由某些库自动构建的JSON文本。在整个编程语言中,有许多库构建JSON文本(一个示例在这里)。

There are many situations where you get JSON text that was automatically built by some library. Throughout the programming languages, there are many libraries that build JSON text (one example is here).

每当库添加一些额外的对象或数组包装时,你可能想要摆脱它们,可能是因为你发送了服务器的JSON和你的代码崩溃,因为它需要一个原始值而不是一个对象(或一个数组)。或者,如果您的JSON是服务器响应,则不希望生成的Javascript代码在对象/数组之间或对象/数组之间不同。在所有这些情况下,展平是有帮助的,因为它可以节省您的时间。您将不得不实现较小的if / elses,并且您可以可靠地期望您的数据结构尽可能平坦。

Whenever libraries add some additional object or array wrappings, you might want to get rid of them maybe because you send the JSON to the server and your code there crashes because it expects a primitive value instead of an object (or an array). Or, if your JSON is a server response, you don't want the resulting Javascript code having to differ between object/array or not object/array. In all these cases, flattening is helpful as it will save you time. You will have to implement lesser if/elses, and you can reliably expect your data structure to be as flat as possible.

另一种改进上述方案代码的方法是以最强大的方式编写代码,因此无法通过多余的包装来崩溃。所以总是期待一些包装并获得它的内容。然后,不需要展平。

The other approach to improve code for the scenario mentioned is to write the code in a maximal robust way so there is no way for it to crash by superfluous wrappings ever. So always expect some wrappers and get it's contents. Then, flattening is not needed.

你知道,这取决于构建JSON的内容以及解析它的内容。该建筑可能超出了您的范围。

You see, it depends on what is building the JSON and what is parsing it. The building may be out of your scope.

这也导致数据模型问题。我曾经使用过需要以不同的方式安静地解析的XML代码,如果有一些XY的0个条目,或者某些XY有> 0个条目。拥有一个允许有一些XY的0或更多条目的包装将使生活更容易。这些是数据模型的定义。

This leads also to data model questions. I've worked with XML code that needed to be parsed quiet a different way if there where 0 entries of some XY, or if there were >0 entries of some XY. Having a wrapper that is allowed to have 0 or more entries of some XY will make live easier. These are data model desicions.

在JSON代表我手动组合的对象结构的所有情况下,我希望不要改变。如此扁平化我设计的细节会令人不安。标准操作到目前为止我看到它们不需要展平(例如 JSON.stringify() json_encode()等等)

In all cases where the JSON represents an object structure that I've combined manually, I expect it not to change. So flattening something I've designed in detail would be disturbing. Standard operations as far I've seen them do not need flattening (e.g. JSON.stringify(), json_encode() etc.)

这篇关于为什么以及何时需要展平JSON对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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