简体   繁体   English

如何改善JSON模式?

[英]How can I improve my JSON schema?

I have this JSON here: 我在这里有这个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 ]
  }]

which represents a list of points in a graph. 代表图中的点列表。 In this case we have three points for a given timestamp so this JSON describes three plots (three channels). 在这种情况下,对于给定的时间戳,我们有3个点,因此此JSON描述了3个图(三个通道)。

This holds for normal channel (ie a list of simple float values). 这适用于普通通道(即,简单浮点值的列表)。 So far so good. 到现在为止还挺好。

Unfortunately, in some cases we have the so called "minmax" channels in which each point is represented as a pair of float! 不幸的是,在某些情况下,我们拥有所谓的“最小最大”通道,其中每个点都表示为一对浮点!

Let's take the last example and replace the middle channel (the second one) as a "minmax" channel. 让我们以最后一个示例为例,并将中间通道(第二个通道)替换为“ minmax”通道。 The problem here is that I don't know how to JSON-ize the pair of float for that "minmax" channel without ambiguity. 这里的问题是,我不知道如何毫不含糊地为“ minmax”通道的浮点对进行JSON大小化。

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

What's the best way to do it? 最好的方法是什么? Can I have unnamed arrays inside other arrays? 我可以在其他数组中使用未命名的数组吗? And what about deserializing such JSON in a Javascript object? 那么如何在Javascript对象中反序列化此类JSON呢?

You could nest an array in this part, like so: 您可以在此部分嵌套一个数组,如下所示:

Without minimax: 没有minimax:

  {
    "HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
    "DateTime": "2014-05-13T09:24:49",
    "ChannelsValues": [ [0.449212707644754], [1.11111], [1.452433] ]
  },

With minimax: 对于minimax:

  {
    "HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
    "DateTime": "2014-05-13T09:24:49",
    "ChannelsValues": [ [0.449212707644754], [1.11111, 1.63245345], [1.452433] ]
  },

Then on parsing, you would only have to check how many array elements the second channel has, that way you can determine if its minimax or not. 然后在解析时,您只需要检查第二个通道具有多少个数组元素,就可以确定第二个通道的minimax与否。 Of course it works exactly the same with channel one and three. 当然,它与第一和第三频道完全相同。

When typed, an array is represented as open and closed brackets: []which may contain a comma-separated list of values[ "a", "b", "c" ]. 键入时,数组用方括号括起来:[]可能包含以逗号分隔的值列表[“ a”,“ b”,“ c”]。 These values can be any JSON data type: string, number,boolean, object, array, null. 这些值可以是任何JSON数据类型:字符串,数字,布尔值,对象,数组,空值。 Source: https://www.json.com/json-array 资料来源: https : //www.json.com/json-array

If channels have some meaning, you could also consider following schema with first , second and third names replaced to better describe the channel. 如果通道具有某些含义,您还可以考虑使用以下模式替换firstsecondthird名称,以更好地描述通道。

{
    "HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
    "DateTime": "2014-05-13T09:24:49",
    "Channels": {
        "first": {
            "value": -5.449212707644754
        },
        "second": {
            "min": 0.312,
            "max": 1.2312321
        },
        "third": {
            "value": -1.452433
        }
    }
}

Adventage of that solution is better readability. 该解决方案的出现是为了提高可读性。 It's obvious what mean min and max . 显然, minmax是什么意思。 If you use nested arrays, no one will know that it's a "minmax" channel 如果使用嵌套数组,没有人会知道这是一个“最小最大”通道

Make your channel values an array of objects. 使通道值成为对象数组。 This way you can name the properties of those objects rather than rely on their position to indicate their meaning. 这样,您可以命名那些对象的属性,而不是依靠它们的位置来指示它们的含义。 eg 例如

{
    "HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
    "DateTime": "2014-05-13T09:24:49",
    "ChannelsValues": [
        { 
            value: -2.449212707644754
        },
        { 
            min: -2.449212707644754,
            max: 4.678432425151,
        },
        { 
            value: 1.452433
        }
    ]
}

Regarding JSON data size, most web servers will allow you to enable compression of HTTP responses and browsers will decompress automatically. 关于JSON数据大小,大多数Web服务器将允许您启用HTTP响应的压缩,浏览器将自动解压缩。 This is super easy to enable on the server side. 这在服务器端非常容易启用。 Compressed JSON is just as small as a binary protocol such as Google Protocol Buffers. 压缩的JSON与二进制协议(如Google Protocol Buffers)一样小。 In this case there will be lots of redundancy in the JSON so it should zip up extremely well. 在这种情况下,JSON中会有很多冗余,因此它应该压缩得非常好。 You could also make the property names single characters as well. 您还可以将属性名称也设置为单个字符。 eg v for value, n for min and x for max. 例如,v代表值,n代表最小值,x代表最大值。

{
    "HistoryId": "bf39d7cfca8536b3e00e5355dff25a97715bc439a5c73d41d10a9eb0c45f2e68",
    "DateTime": "2014-05-13T09:24:49",
    "ChannelsValues": [
        { 
            v: -2.449212707644754
        },
        { 
            n: -2.449212707644754,
            x: 4.678432425151,
        },
        { 
            v: 1.452433
        }
    ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM