【发布时间】:2018-04-13 13:55:20
【问题描述】:
假设我的数据库中有两个表,employee 和 car 这样定义。
员工:
+--------------+------------+
| col_name | data_type |
+--------------+------------+
| eid | int |
| name | string |
| salary | int |
| destination | string |
+--------------+------------+
汽车:
+------------+----------------+
| col_name | data_type |
+------------+----------------+
| cid | int |
| name | string |
| model | string |
| cylinders | int |
| price | int |
+------------+----------------+
我想将此模式导出到 JSON 对象,以便我可以基于表填充 HTML 下拉菜单 - 例如,table 菜单将具有 employee 和 car。选择 employee 将使用与该表对应的列名和类型填充另一个下拉列表。
考虑到这个用例,数据库的最佳 json 表示会是这样吗?
{
"employee": {
"salary": "int",
"destination": "string",
"eid": "int",
"name": "string"
},
"car": {
"price": "int",
"model": "string",
"cylinders": "int",
"name": "string",
"cid": "int"
}
}
编辑: 还是这样更合适?
{
"employee": [
{
"type": "int",
"colname": "eid"
},
{
"type": "string",
"colname": "name"
},
{
"type": "int",
"colname": "salary"
},
{
"type": "string",
"colname": "destination"
}
],
"car": [
{
"type": "int",
"colname": "cid"
},
{
"type": "string",
"colname": "name"
},
{
"type": "string",
"colname": "model"
},
{
"type": "int",
"colname": "cylinders"
},
{
"type": "int",
"colname": "price"
}
]
}
【问题讨论】:
-
您的问题到底是什么,如果示例 json 是最佳表示形式?鉴于您描述的用例,我认为这是一个合适的表示。
-
谢谢@martink - 我已经添加了另一个 JSON 结构 - 如果可以选择哪个最合适?