原始数据
数据库里有这样的一些数据
| _id | p_id | parm_key | parm_value | parm_type |
|---|---|---|---|---|
| 0 | -1 | template | object | |
| 10 | 0 | name | template-name | string |
| 20 | 0 | inputs | object | |
| 201 | 20 | parameters | [object] | |
| 2011 | 201 | name | this-is-name-1 | string |
| 2012 | 201 | value | this-is-value-1 | string |
| 2013 | 201 | name | this-is-name-2 | string |
| 2014 | 201 | value | this-is-value-2 | string |
其中:
- id 和 p_id 是表示层级关系的
- parm_key 对应的是某个对象的属性名称
- parm_value 是 parm_type 为 string 时的属性值
需求
要根据上述的数据结构,生成以下格式的 json 对象或者 json 字符串:
{
"template": {
"name": "template-name",
"inputs": {
"parameters": [
{
"name": "this-is-name-1",
"value": "this-is-value-1"
},
{
"name": "this-is-name-2",
"value": "this-is-value-2"
}
]
}
}
}
用什么样的方法能够做到这种转换呢?