首页 > 解决方案 > 格式化 SPA 使用的 JSON 数据的最佳方法是什么?

问题描述

我正在与一位朋友一起开发单页应用程序(在 React 中,但我相信框架并不重要,同样的问题也适用于 Angular)。

有一个包含 2 个表的数据库:

两个表在数据库中以多对多关系连接。

我们的不同之处在于我们应该如何将数据从后端传递到前端(更准确地说,CarManagementComponent 将让用户处理汽车/功能(编辑/更新/删除等))。我们希望能够在之前执行多个操作,实际上是向后端发送请求以更新数据库,以便用户拥有类似桌面应用程序的界面体验。

请记住,数据库中有更多表,但为了示例的简单性,我们在这里只讨论其中的 2 个。

1)我的方法:

{
    "Features": [
        {
            "Id": 0,
            "Price": 3000,
            "Name": "led lights",
            "Color": "transparent",
            "Brand": "Valeo",
            "Guarantee": 12
        },
        {
            "Id": 1,
            "Price": 1000,
            "Name": "air conditioning",
            "Color": "",
            "Brand": "Bosch",
            "Guarantee": 12
        },
        {
            "Id": 2,
            "Price": 600,
            "Name": "tinted windows",
            "Color": "",
            "Brand": "Bosch",
            "Guarantee": 36
        }
    ],
    "Cars": [
        {
            "Id": 0,
            "Name": "Ford Mustang GT",
            "Weight": 2210,
            "Features":[
                {
                    "Id": 0, // id of many-to-many relations record
                    "FeatureId": 2
                },
                {
                    "Id": 1, // id of many-to-many relations record
                    "FeatureId": 1
                }
            ]
        },
        {
            "Id": 1,
            "Name": "Volkswagen Arteon",
            "Weight": 1650,
            "Features":[
                {
                    "Id": 2, // id of many-to-many relations record
                    "FeatureId": 2
                }
            ]
        }         
    ]
}

2)我朋友的做法:

{
    "Cars": [
        {
            "Id": 0,
            "Name": "Ford Mustang GT",
            "Weight": 2210,
            "Features": [
                {
                    "Id": 1,
                    "Price": 1000,
                    "Name": "air conditioning",
                    "Color": "",
                    "Brand": "Bosch",
                    "Guarantee": 12
                },
                {
                    "Id": 2,
                    "Price": 600,
                    "Name": "tinted windows",
                    "Color": "",
                    "Brand": "Bosch",
                    "Guarantee": 36
                }
            ]
        },
        {
            "Id": 1,
            "Name": "Volkswagen Arteon",
            "Weight": 1650,
            "Features": [
                {
                    "Id": 2,
                    "Price": 600,
                    "Name": "tinted windows",
                    "Color": "",
                    "Brand": "Bosch",
                    "Guarantee": 36
                }
            ]
        }
    ]
}

我相信第一种方法更好,因为:

我的朋友说第二种方法更好,因为:

你怎么看?哪种解决方案更好?也许他们都在某些领域?也许我们还没有想到第三种解决方案?

标签: jsonangularreactjsarchitecture

解决方案


我想说我最喜欢的方法是你的,主要原因有两个:

  1. 请记住,HTTP 请求中的数据重复很糟糕,您的方法是避免它。
  2. 您让FeatureId汽车内部成为对象,并且足以以有效的性能 O(N) 获得该功能。

为了使它更好,您可以将您的功能结构更改为:

"Features": {
       0: { // <- If the id is unique, you can use it as a key.
            "Id": 0,
            "Price": 3000,
            "Name": "led lights",
            "Color": "transparent",
            "Brand": "Valeo",
            "Guarantee": 12
        },
        1: {
            "Id": 1,
            "Price": 1000,
            "Name": "air conditioning",
            "Color": "",
            "Brand": "Bosch",
            "Guarantee": 12
        },
        2: {
            "Id": 2,
            "Price": 600,
            "Name": "tinted windows",
            "Color": "",
            "Brand": "Bosch",
            "Guarantee": 36
        }
    },

这样,您可以获得 O(1) 中的特征。


推荐阅读