简体   繁体   English

如何在JSON中表达类型?

[英]How to express types in JSON?

I want to store tree data using JSON, but I can't figure out how to express types nicely. 我想使用JSON存储树数据,但是我不知道如何很好地表达类型。

Below is how the XML for a hypothetical game involving cats and mice could look like: 以下是涉及猫和老鼠的假设游戏的XML外观:

<level>
  <entities>
    <cat>
      <direction>right</direction>
      <position x="100" y="100"/>
    </cat>
    <mouse>
      <direction>right</direction>
      <position x="50" y="50"/>
    </mouse>
  </entities>
</level>

The best JSON I can come up with for this is as follows: 为此,我能想到的最好的JSON如下:

{
  "entities": [
    {
      "type": "cat",
      "direction": "right",
      "position": {"x": 100, "y": 100}
    },
    {
      "type": "mouse",
      "direction": "right",
      "position": {"x": 50, "y": 50}
    }
  ]
}

Here I specify the type explicitly. 在这里,我明确指定类型。 It seems like XML simply is a better fit for this, but unfortunately, I can't really use it, so I'm wondering if there's a nicer way to do it with JSON. 看起来XML似乎更适合于此,但是不幸的是,我不能真正使用它,所以我想知道是否有更好的方法来使用JSON。

Inspired by Lua's table constructors, I came up with the following, which is clearly not JSON, but JS is actually fine in my case (this data is not intended to be transfered, it's just for a tree of objects): 在Lua的表构造函数的启发下,我想到了以下内容,它显然不是JSON,但就我而言,JS实际上是可以的(该数据不打算传输,仅用于对象树):

{
  "entities": [
    cat({
      "direction": "right",
      "position": {"x": 100, "y": 100}
    }),
    mouse({
      "direction": "right",
      "position": {"x": 50, "y": 50}
    })
  ]
};

But, as in Lua, the downside is that I actually have to define all possible entities as functions somewhere: 但是,就像在Lua中一样,缺点是我实际上必须将所有可能的实体定义为某个地方的函数:

function cat(options) {
  options.type = "cat";
  return options;
}

function mouse(options) {
  options.type = "mouse";
  return options;
}

What do you think, is there a better way to express types in JSON than to have an attribute for that? 您怎么看,有比在JSON中表达类型更好的方法吗? Is my hack going too far? 我的技巧太过分了吗?

If your types are unique, you could use this 如果您的类型是唯一的,则可以使用此

var entities = {
    cat : {
        direction: "right",
        position: {x: 100, y: 100}
    },
    mouse : {
        direction: "right",
        position: {x: 50, y: 50}
    }
};

Then if you want to access different types, use this 然后,如果要访问其他类型,请使用此

Object.keys(entities) // outputs ["cat","mouse"]

EDIT after your comment : 评论后编辑:

What about this way ? 那这样呢?

var entities = {
    cat : [{
        direction: "right",
        position: {x: 100, y: 100}
    },{
        direction: "top",
        position: {x: 150, y: 150}
    }],
    mouse : [{
        direction: "right",
        position: {x: 50, y: 50}
    }]
};

Perhaps you should rethink your XML: 也许您应该重新考虑XML:

<entities>
  <cat>
   <direction>right</direction>
   <position x="100" y="100"/>
  </cat>
  <mouse>
   <direction>right</direction>
   <position x="50" y="50"/>
  </mouse>
</entities>

Would be better expressed as: 最好表示为:

<entities>
  <animal>
   <tpye>cat</type>
   <direction>right</direction>
   <position x="100" y="100"/>
  </animal>
  <animal>
   <tpye>cat</type>
   <direction>right</direction>
   <position x="50" y="50"/>
  </animal>
</entities>

Which could be represented by: 可以表示为:

[{"type":"cat","direction":"right", position:{"x":"50","y":50"}},{"type":"mouse","direction":"right", position:{"x":"50","y":50"}}]

Assuming, that you have an "array of animals". 假设您有“一群动物”。

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

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