如何改进我的 JSON 架构?
How can I improve my JSON schema?
我这里有这个JSON:
[{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"ChannelsValues": [ -2.449212707644754, 2.3458776596, 1.452433 ]
},
{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"ChannelsValues": [ 1.449212707644754, 2.3458776596, 1.452433 ]
},
{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"ChannelsValues": [ 0.449212707644754, 2.3458776596, 1.452433 ]
},
{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"ChannelsValues": [ 0.449212707644754, 2.3458776596, 1.452433 ]
},
{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"ChannelsValues": [ 0.449212707644754, 2.3458776596, 1.452433 ]
},
{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"ChannelsValues": [ 0.449212707644754, 2.3458776596, 1.452433 ]
},
{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"ChannelsValues": [ 0.449212707644754, 2.3458776596, 1.452433 ]
},
{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"ChannelsValues": [ 0.449212707644754, 2.3458776596, 1.452433 ]
},
{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"ChannelsValues": [ -5.449212707644754, 2.3458776596, -1.452433 ]
}]
表示图形中的点列表。在这种情况下,对于给定的时间戳,我们有三个点,因此 JSON 描述了三个图(三个通道)。
这适用于普通频道(即简单浮点值列表)。
到目前为止一切顺利。
不幸的是,在某些情况下,我们有所谓的 "minmax" 通道,其中每个点都表示为一对浮点数!
让我们以最后一个例子为例,将中间通道(第二个)替换为 "minmax" 通道。这里的问题是我不知道如何 JSON-ize 那个 "minmax" 通道的一对浮点数而没有歧义。
[{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"ChannelsValues": [ -2.449212707644754, ???, 1.452433 ]
},
{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"ChannelsValues": [ 1.449212707644754, ???, 1.452433 ]
},
{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"ChannelsValues": [ 0.449212707644754, ???, 1.452433 ]
},
{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"ChannelsValues": [ 0.449212707644754, ???, 1.452433 ]
},
{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"ChannelsValues": [ 0.449212707644754, ???, 1.452433 ]
},
{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"ChannelsValues": [ 0.449212707644754, ???, 1.452433 ]
},
{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"ChannelsValues": [ 0.449212707644754, ???, 1.452433 ]
},
{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"ChannelsValues": [ 0.449212707644754, ???, 1.452433 ]
},
{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"ChannelsValues": [ -5.449212707644754, ???, -1.452433 ]
}]
最好的方法是什么?我可以在其他数组中包含未命名的数组吗?
那么在 Javascript 对象中反序列化这样的 JSON 呢?
你可以在这部分嵌套一个数组,像这样:
没有极小极大值:
{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"ChannelsValues": [ [0.449212707644754], [1.11111], [1.452433] ]
},
极小极大:
{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"ChannelsValues": [ [0.449212707644754], [1.11111, 1.63245345], [1.452433] ]
},
然后在解析时,你只需要检查第二个通道有多少个数组元素,这样你就可以确定它是否是极小极大。当然,它与通道一和通道三完全一样。
When typed, an array is represented as open and closed brackets:
[]which may contain a comma-separated list of values[ "a", "b", "c" ].
These values can be any JSON data type: string, number,boolean,
object, array, null.
Source: https://www.json.com/json-array
如果频道有一定意义,您还可以考虑将以下架构替换为 first
、second
和 third
名称以更好地描述频道。
{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"Channels": {
"first": {
"value": -5.449212707644754
},
"second": {
"min": 0.312,
"max": 1.2312321
},
"third": {
"value": -1.452433
}
}
}
该解决方案的优点是可读性更好。很明显 min
和 max
是什么意思。如果你使用嵌套数组,没有人会知道这是一个 "minmax" 频道
使您的频道值成为一个对象数组。这样您就可以命名这些对象的属性,而不是依赖它们的位置来指示它们的含义。例如
{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"ChannelsValues": [
{
value: -2.449212707644754
},
{
min: -2.449212707644754,
max: 4.678432425151,
},
{
value: 1.452433
}
]
}
关于 JSON 数据大小,大多数 Web 服务器将允许您启用 HTTP 响应压缩,浏览器将自动解压缩。这在服务器端非常容易启用。压缩后的 JSON 与 Google Protocol Buffers 等二进制协议一样小。在这种情况下,JSON 中会有很多冗余,因此它应该非常好地压缩。您也可以将 属性 名称设为单个字符。例如v 为值,n 为最小值,x 为最大值。
{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"ChannelsValues": [
{
v: -2.449212707644754
},
{
n: -2.449212707644754,
x: 4.678432425151,
},
{
v: 1.452433
}
]
}
我这里有这个JSON:
[{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"ChannelsValues": [ -2.449212707644754, 2.3458776596, 1.452433 ]
},
{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"ChannelsValues": [ 1.449212707644754, 2.3458776596, 1.452433 ]
},
{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"ChannelsValues": [ 0.449212707644754, 2.3458776596, 1.452433 ]
},
{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"ChannelsValues": [ 0.449212707644754, 2.3458776596, 1.452433 ]
},
{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"ChannelsValues": [ 0.449212707644754, 2.3458776596, 1.452433 ]
},
{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"ChannelsValues": [ 0.449212707644754, 2.3458776596, 1.452433 ]
},
{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"ChannelsValues": [ 0.449212707644754, 2.3458776596, 1.452433 ]
},
{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"ChannelsValues": [ 0.449212707644754, 2.3458776596, 1.452433 ]
},
{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"ChannelsValues": [ -5.449212707644754, 2.3458776596, -1.452433 ]
}]
表示图形中的点列表。在这种情况下,对于给定的时间戳,我们有三个点,因此 JSON 描述了三个图(三个通道)。
这适用于普通频道(即简单浮点值列表)。 到目前为止一切顺利。
不幸的是,在某些情况下,我们有所谓的 "minmax" 通道,其中每个点都表示为一对浮点数!
让我们以最后一个例子为例,将中间通道(第二个)替换为 "minmax" 通道。这里的问题是我不知道如何 JSON-ize 那个 "minmax" 通道的一对浮点数而没有歧义。
[{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"ChannelsValues": [ -2.449212707644754, ???, 1.452433 ]
},
{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"ChannelsValues": [ 1.449212707644754, ???, 1.452433 ]
},
{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"ChannelsValues": [ 0.449212707644754, ???, 1.452433 ]
},
{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"ChannelsValues": [ 0.449212707644754, ???, 1.452433 ]
},
{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"ChannelsValues": [ 0.449212707644754, ???, 1.452433 ]
},
{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"ChannelsValues": [ 0.449212707644754, ???, 1.452433 ]
},
{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"ChannelsValues": [ 0.449212707644754, ???, 1.452433 ]
},
{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"ChannelsValues": [ 0.449212707644754, ???, 1.452433 ]
},
{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"ChannelsValues": [ -5.449212707644754, ???, -1.452433 ]
}]
最好的方法是什么?我可以在其他数组中包含未命名的数组吗? 那么在 Javascript 对象中反序列化这样的 JSON 呢?
你可以在这部分嵌套一个数组,像这样:
没有极小极大值:
{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"ChannelsValues": [ [0.449212707644754], [1.11111], [1.452433] ]
},
极小极大:
{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"ChannelsValues": [ [0.449212707644754], [1.11111, 1.63245345], [1.452433] ]
},
然后在解析时,你只需要检查第二个通道有多少个数组元素,这样你就可以确定它是否是极小极大。当然,它与通道一和通道三完全一样。
When typed, an array is represented as open and closed brackets: []which may contain a comma-separated list of values[ "a", "b", "c" ]. These values can be any JSON data type: string, number,boolean, object, array, null. Source: https://www.json.com/json-array
如果频道有一定意义,您还可以考虑将以下架构替换为 first
、second
和 third
名称以更好地描述频道。
{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"Channels": {
"first": {
"value": -5.449212707644754
},
"second": {
"min": 0.312,
"max": 1.2312321
},
"third": {
"value": -1.452433
}
}
}
该解决方案的优点是可读性更好。很明显 min
和 max
是什么意思。如果你使用嵌套数组,没有人会知道这是一个 "minmax" 频道
使您的频道值成为一个对象数组。这样您就可以命名这些对象的属性,而不是依赖它们的位置来指示它们的含义。例如
{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"ChannelsValues": [
{
value: -2.449212707644754
},
{
min: -2.449212707644754,
max: 4.678432425151,
},
{
value: 1.452433
}
]
}
关于 JSON 数据大小,大多数 Web 服务器将允许您启用 HTTP 响应压缩,浏览器将自动解压缩。这在服务器端非常容易启用。压缩后的 JSON 与 Google Protocol Buffers 等二进制协议一样小。在这种情况下,JSON 中会有很多冗余,因此它应该非常好地压缩。您也可以将 属性 名称设为单个字符。例如v 为值,n 为最小值,x 为最大值。
{
"HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
"DateTime": "2014-05-13T09:24:49",
"ChannelsValues": [
{
v: -2.449212707644754
},
{
n: -2.449212707644754,
x: 4.678432425151,
},
{
v: 1.452433
}
]
}