Array of objects vs Object of Objects

后端 未结 6 1103
有刺的猬
有刺的猬 2020-12-13 05:56

The issue is to decided the trade offs between following notations:

JSON based:

\"users\": {
    \"id1\": {
        \"id\": \"id1\",         


        
6条回答
  •  难免孤独
    2020-12-13 06:34

    Along with all the above technical difference, I think there is a fundamental difference in the purpose and meaning of an Object and an Array.

    1. The properties of an object DESCRIBE/DEFINE the object whereas
    2. The elements of an array does NOT DESCRIBE/DEFINE the array, on the contrary the array defines what it's contents are. Do note - I am not talking about technical aspects. You can have any combinations technically but semantically each has its purpose.

      • For example a card holder. Each card does NOT DESCRIBE/DEFINE the card-holder. But the card holder does define it's purpose that it holds only cards/

      • An Object is used to represent an entity and its properties DESCRIBE/DEFINE the entity. Take the same example of a Card. A card has properties like color, number which DESCRIBE/DEFINE what the card is.

    For your above example:

    1. Each object which represents a person is defined by the properties id, firstName and lastName.

    2. A list of these persons cannot be an object of objects because each id does not describe the object of objects. So

      "users":[ { "id":"id", "key2":"value2", "key3":"value3" }, { "id":"id", "key2":"value2", "key3":"value3" } ]

    is a better representation than

    "users": {
        "id1": {
            "id": "id1",
            "firstname": "firstname1",
            "lastname": "lastname1"
        },
        "id2": {
            "id": "id2",
            "firstaame": "firstname2",
            "lastname": "lastname2"
        }
    }
    

    even though technically you can use either. I hope i was able to convey(put into words) my thinking in the right manner.

提交回复
热议问题