【问题标题】:Is there a way to include null columns in my json output when using apex_json使用 apex_json 时,有没有办法在我的 json 输出中包含空列
【发布时间】:2021-09-11 23:38:27
【问题描述】:

我的 19c 数据库中有许多存储过程,我正在使用 apex_json 包将游标的结果转换为 json 以获取 rest api。

最近我发现我选择的一些列中有空值,所以当生成 json 时,该列不包含在输出中。不幸的是,当我使用 knnockout 将 json 绑定到我的网页时,viewmodel javascript 失败,因为在某些情况下没有要绑定的列。

所以我的问题是,有没有办法在打开它或编写它时配置 apex_json 以告诉包我想在输入光标中的空列的 json 输出中包含“null”或类似的东西?

示例存储过程:

create or replace procedure  my_demo
) as
    l_cur sys_refcursor;
begin
    open l_cur for
        select name,email,phonenumber
    from temp
    order by name;

-- return the resultset in json format
apex_json.open_object;
apex_json. write('data',l_cur);
apex_json.close_object;

end my_demo;

当前输出:

{
"data" : [
{"name":"Barney","email":"barney@example.com","phonenumber":"555.1212"},
{"name":"Fred","email":"fred@example.com"},
{"name":"Robert","email"robert@example.com","phonenumber":"555.2323"}
]}

预期输出:

{
"data" : [
{"name":"Barney","email":"barney@example.com","phonenumber":"555.1212"},
{"name":"Fred","email":"fred@example.com","phonenumber":""},
{"name":"Robert","email"robert@example.com","phonenumber":"555.2323"}
]}

【问题讨论】:

    标签: json rest plsql null


    【解决方案1】:

    通常你不能使用 null 作为值,但是你可以使用一个空字符串,它应该很容易在你的程序中转换为 null 值。 应该是这样的:

    [{数据{ 姓名: '' } }]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-16
      • 1970-01-01
      • 2020-07-25
      相关资源
      最近更新 更多