从 Oracle DB 构建 JSON 字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17541032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 19:56:22  来源:igfitidea点击:

Constructing JSON string from Oracle DB

jsonoracle

提问by AnaMaria

I have a Web application which gets it data from a JSON string. The JSON is in the following format

我有一个 Web 应用程序,它从 JSON 字符串中获取数据。JSON 格式如下

{
    "contacts": [{
        "type": "contact",
        "name": "John Doe",
        "contact": 1,
        "links": ["Spouse", "Friends","Jane Doe","Harry Smith"]
                 }]
}

Now this is a sample data. My actual DB is in Oracle. My question would be how do I construct this JSON from Oracle.

现在这是一个示例数据。我的实际数据库在 Oracle 中。我的问题是如何从 Oracle 构建这个 JSON。

采纳答案by manadart

This is the best method I've come across: http://ora-00001.blogspot.sk/2010/02/ref-cursor-to-json.html.

这是我遇到的最好的方法:http: //ora-00001.blogspot.sk/2010/02/ref-cursor-to-json.html

To summarise:

总结一下:

  • Use the DBMS_XMLGENpackage to generate XML from a SYS_REFCURSOR.
  • Then transform it using this XSLT.
  • 使用该DBMS_XMLGEN包从 .xml 文件生成 XML SYS_REFCURSOR
  • 然后使用此XSLT对其进行转换。

I like it because there's no manual generation and because you have the option of returning XML too by skipping the final transformation.

我喜欢它,因为没有手动生成,并且因为您也可以选择跳过最终转换来返回 XML。