【发布时间】:2022-01-24 12:55:26
【问题描述】:
目前,我正在使用 oracle 为后端目的生成一些带有数据的 json,并且我正在努力处理必须手动处理的复杂且重复的结构。
例如我有这个对象数组:
{
"infoColumnsWidgets": [
{
"widgetNamespace": "mot",
"widgetName": "info_column",
"orderNumber": 1,
"navigateToPage": null,
"widgetData": {
"title": "Fact",
"textPattern": "$v0",
"values": [
{
"id": "v0",
"type": "int",
"value": "200000"
}
]
}
},
{
"widgetNamespace": "mot",
"widgetName": "info_column",
"orderNumber": 2,
"navigateToPage": null,
"widgetData": {
"title": "Plan",
"textPattern": "$v0",
"values": [
{
"id": "v0",
"type": "int",
"value": "200000"
}
]
}
},
{
"widgetNamespace": "mot",
"widgetName": "info_column",
"orderNumber": 3,
"navigateToPage": null,
"widgetData": {
"title": "Prognosis",
"textPattern": "$v0",
"values": [
{
"id": "v0",
"type": "int",
"value": "100"
}
]
}
}
]
}
当然,我在循环中生成它,但这种结构经常出现,我更愿意将它放入某个函数中以执行以下操作:
function f_getTest return clob as
v_res clob;
begin
apex_json.initialize_clob_output;
apex_json.open_object;
apex_json.open_object('infoColumnsWidgets');
for rec in (select * from some_table_data)
loop
apex_json.write_raw(f_getWidgetJson(rec.param));
end loop;
apex_json.close_object;
apex_json.close_all;
v_res := apex_json.get_clob_output;
apex_json.free_output;
return v_res;
end;
但据我所知,没有选项可以使用 apex_json 将一个 json 放入另一个中。我可以尝试一些奇怪的解决方法,在最终的 clob 中放置一些占位符并替换它们,但不,我不想,请不要让我这样做。
非常欢迎任何想法
【问题讨论】:
-
你使用的是什么版本的oracle?目前还不清楚您到底想做什么 - 您是想从关系表生成 json 还是存储了需要修改的 json?
-
Oracle 12c,apex - 5,据我所知。我使用 apex_json 函数成功生成了上面展示的 json:打开数组、添加对象、放置一些值、关闭对象和数组,你知道的。我想做的就是把重复的部分变成函数;在生成时在循环中调用它们。
-
我已经创建了一个单独的函数来生成一个我想放入“main”json函数的对象,但是我没有找到任何方法将它正确地添加到结构中。
-
从 12c 第 2 版开始,您可以在数据库中原生使用 sql 生成 json - 这比使用 apex_json 容易得多
-
或者,您也可以只编写 sql 语句并使用 ORDS 将它们公开为 json(作为一项安静的服务) - 您只需要检查您的 ords 版本中的可能性