假设我的数据库中有两个表, 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"
}
]
}
1条答案
按热度按时间jecbmhm31#
在第一个示例中,所有数据都存储在对象中。假设结构存储在var中
mytables
,你可以用Object.keys(mytables)
,返回['employee', 'car']
. 内柱等效值:Object.keys(mytables['employee'].cols)
退货['salary','destination','eid','name']
.在第二个示例中,我建议也将表作为列存储在数组中,如
然后您可以通过访问
mytables[i].name
```for (t in tables){
console.log(tables[t].name);
for (c in tables[t].cols)
console.log(" - ",tables[t].cols[c].colname, ": ", tables[t].cols[c].type);
}