简体   繁体   English

为例如persondata 编写JSON 的正确方法是什么?

[英]What is the right way to write JSON for example persondata?

Simple question, what is better?简单的问题,什么更好? Please explain in 1-2 sentences why.请用 1-2 句话解释原因。 I haven't much worked with JSON files but think in future I will do more often.我对 JSON 文件的处理不多,但我认为将来我会更频繁地使用它。 So it's better to start with proper structure, instead of working some time and realize another structure would be better.所以最好从合适的结构开始,而不是工作一段时间然后意识到另一种结构会更好。

"id" as key: “id”作为键:

    {
    "0": {
        "name": "name1",
        "bdate": "bdate1",
        "mail": "mail1",
        "sex": "sex1"
    },
    "1": {
        "name": "name2",
        "bdate": "bdate2",
        "mail": "mail2",
        "sex": "sex2"
    },
    "2": {
        "name": "name3",
        "bdate": "bdate3",
        "mail": "mail3",
        "sex": "sex3"
    }
}

"id" in data:数据中的“id”:

    [
    {
        "id":"0",
        "name": "name1",
        "bdate": "bdate1",
        "mail": "mail1",
        "sex": "sex1"
    },
    {
        "id":"1",
        "name": "name2",
        "bdate": "bdate2",
        "mail": "mail2",
        "sex": "sex2"
    },
    {
        "id":"2",
        "name": "name3",
        "bdate": "bdate3",
        "mail": "mail3",
        "sex": "sex3"
    }
]

Using an object indexed by ID will make it easier to look up objects if there will be circumstances in which you'll have the ID to find in advance.如果在某些情况下您需要提前查找 ID,则使用按 ID 索引的对象将使查找对象变得更加容易。 With that, using your first approach will let you find the object as easily as:有了这个,使用你的第一种方法会让你很容易地找到对象:

const personObj = allObjects[id]

If you used an array instead, you'd have to iterate over the whole array in order to find it:如果您改用数组,则必须遍历整个数组才能找到它:

const personObj = allObjects.find(
  obj => obj.id === id
);

which is an order of magnitude slower (though, that's only relevant if the amount of data is quite large).这要慢一个数量级(不过,这仅在数据量非常大时才相关)。

Of course, make sure an ID uniquely identifies one, and only one, object.当然,请确保一个 ID 唯一标识一个且仅一个对象。

If the IDs happen to exactly correspond to their position in the array - as in this example - you could use the array instead and use allObjects[id] - but that could create problems later if items ever get added or removed from the array.如果 ID恰好与它们在数组中的位置相对应- 如本例所示 - 您可以改用该数组并使用allObjects[id] - 但如果项目从数组中添加或删除,这可能会在以后产生问题。

If you need to look up users by id, then having a map will certainly be quicker.如果您需要通过 id 查找用户,那么拥有地图肯定会更快。 In most cases where you will be looking up values using a key, a map will be the choice that gives the best performance and most straightforward coding.在大多数使用键查找值的情况下,映射将是提供最佳性能和最直接编码的选择。

If you are always having to get every user, then a list may be more convenient, but its easy to convert a map to a list when you need to.如果您总是需要获取每个用户,那么列表可能更方便,但是在需要时可以轻松地将地图转换为列表。

Most languages have straightforward syntax that will allow you to create a list from a map and vice versa.大多数语言都有简单的语法,允许您从地图创建列表,反之亦然。 The cost of conversion is much the same.转换成本大致相同。

Don't forget that a list contains extra information.. ie order that is not available on a map.不要忘记列表包含额外信息.. 即地图上不可用的订单。 So if order is all important.. eg items in a queue or similar, then choose to use a list.因此,如果顺序很重要……例如队列中的项目或类似项目,则选择使用列表。

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

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