如何改进我的JSON架构?

时间:2015-07-22 11:15:51

标签: javascript json serialization json-deserialization

我在这里有这个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呢?

3 个答案:

答案 0 :(得分:1)

您可以在此部分嵌套数组,如下所示:

没有minimax:

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

使用minimax:

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

然后在解析时,您只需要检查第二个通道有多少个数组元素,这样就可以确定它是否为极小极大值。当然,它与第一和第三频道完全相同。

  

键入时,数组表示为打开和关闭括号:   []其中可能包含以逗号分隔的值列表[“a”,“b”,“c”]。   这些值可以是任何JSON数据类型:string,number,boolean,   object,array,null。   资料来源:https://www.json.com/json-array

答案 1 :(得分:0)

如果频道具有某些含义,您还可以考虑使用以下架构,并替换df -ifirstsecond名称以更好地描述频道。

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是什么意思。如果你使用嵌套数组,没有人会知道它是" minmax"信道

答案 2 :(得分:0)

使您的频道值成为一个对象数组。这样,您可以命名这些对象的属性,而不是依赖它们的位置来指示它们的含义。 e.g。

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