用JSON表示DB模式
假设我的数据库中有两个表,employee和car是这样定义的 雇员:用JSON表示DB模式,json,hive,Json,Hive,假设我的数据库中有两个表,employee和car是这样定义的 雇员: +--------------+------------+ | col_name | data_type | +--------------+------------+ | eid | int | | name | string | | salary | int | | destination | string | +--
+--------------+------------+
| col_name | data_type |
+--------------+------------+
| eid | int |
| name | string |
| salary | int |
| destination | string |
+--------------+------------+
汽车:
我想将这个模式导出到一个JSON对象,这样我就可以基于表格填充一个HTML下拉菜单——例如,表格菜单将包含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"
}
]
}
在第一个示例中,所有数据都存储在对象中。假设该结构存储在var mytables中,则可以使用Object.keysmytables获取名称,它返回['employee','car']。与其中的列等效:Object.keysmytables['employee'].cols返回['salary','destination','eid','name'] 在第二个示例中,我建议还将表作为列存储在数组中,如
[name: 'employee',
cols: [ {
"type": "int",
"colname": "cid"
}, ...]
然后,您可以通过访问mytables[i].name轻松地迭代数组并获取名称
如果示例json是最佳表示,您的问题是什么?考虑到您描述的用例,我认为这是一个合适的表示形式。谢谢@martink-我添加了另一个JSON结构-如果有选择,哪一个最合适?
[name: 'employee',
cols: [ {
"type": "int",
"colname": "cid"
}, ...]
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);
}