当前位置:网站首页>Oracle和JSON的結合
Oracle和JSON的結合
2022-07-01 10:51:00 【bisal(Chen Liu)】
很多人對JSON不陌生,JSON(JavaScript Object Notation)是一種輕量級的數據交換格式,他基於ECMAScript(European Computer Manufacturers Association,歐洲計算機協會制定的js規範)的一個子集,采用完全獨立於編程語言的文本格式來存儲和錶示數據。簡潔和清晰的層次結構使得JSON成為理想的數據交換語言。易於人閱讀和編寫,同時也易於機器解析和生成,並有效地提昇網絡傳輸效率。
簡而言之,JSON是一種存儲和交換數據的語法。JSON本質就是字符串,JSON數據是在語言裏面使用的,無論是Java、C#,還是js,都可以使用JSON。
JSON就是一組鍵值對的數據對象,在JavaScript裏面就是JavaScript的對象,在Java中就是Java的JSON對象(類/VO)。JSON數據寫為名稱/值對,名稱/值由字段名稱構成,後跟冒號和值,一般的格式,如下所示,
name={name01:“test1”,name02={ name02:“test2”,name021:“test3”} }在JSON中,值必須是以下數據類型之一,
字符串
數字
對象(JSON對象)
數組
布爾
Null
JSON的值不可以是以下數據類型之一,
函數
日期
undefined
有開發經驗的朋友可能以前用過XML作為數據傳輸的格式,如下就是一個XML數據格式的例子,

這是用JSON展示相同數據的例子,

可以看到,JSON簡單的語法格式和清晰的層次結構明顯要比XML容易閱讀,並且在數據交換方面,由於JSON所使用的字符要比XML少得多,可以大大得節約傳輸數據所占用的帶寬,傳輸效率上,會高於XML。
Oracle從12c開始就支持JSON的存儲和檢索,每個版本中都對相關的功能有所增强,例如一些JSON的限制,允許的長度等特性就會有改進,例如12c的描述中,JSON search index支持64字節,
https://docs.oracle.com/en/database/oracle/oracle-database/12.2/adjsn/oracle-json-restrictions.html#GUID-1DB81125-54A7-4CB6-864B-78E0E7E407C9

19c的描述中,JSON search index已經能支持255字節,
https://docs.oracle.com/en/database/oracle/oracle-database/19/adjsn/oracle-json-restrictions.html#GUID-1DB81125-54A7-4CB6-864B-78E0E7E407C9

所以當我們使用這些特性的時候,需要關注不同版本的支持差別。
Oracle中我們可以用json_object()函數來將檢索結果轉成JSON,如下所示,

還可以自定義key,

可以將檢索結果存儲為列錶,

還支持做排序,

只使用json_object(),每條記錄是單獨的一個json,如果要將所有的記錄生成一個json,可以結合json_arrayagg一起用,如下所示,

但很可能出現這個錯誤,返回記錄數據多,提示最大值4000,

這是因為json_arrayagg()返回的類型有兩種,varchar2和clob,如果不顯式指定,用的就是varchar2,如果超過限度,可以改用clob類型,就會輸出更多的內容,
https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/JSON_ARRAYAGG.html#GUID-6D56077D-78DE-4CC0-9498-225DDC42E054

增加returning clob子句,就可以顯示了,

和JSON相關的函數還有很多,有需要可以了解,

對於一些基本的JSON操作需求,Oracle還是可以滿足的,這就避免了通過程序或者SQL拼接JSON的操作,從便利性上,有一定的幫助。
參考資料,
https://baike.baidu.com/item/JSON/2462549?fr=aladdin
https://blog.csdn.net/weixin_44600235/article/details/107630279
近期更新的文章:
《小白學習MySQL - 增量統計SQL的需求 - 開窗函數的方案》
近期的熱文:
文章分類和索引:
边栏推荐
- php 实现抽奖功能
- 商城小程序源码开源版-可二开
- The Lantern Festival is held on the fifteenth day of the first month, and the Lantern Festival begins to celebrate the reunion
- How to get the maximum value of column two and regenerate the table when the SQL Server column one is the same
- CCNP Part XII BGP (IV)
- Half of 2022 has passed, isn't it sudden?
- 【MPC】①二次规划问题MATLAB求解器quadprog
- JS基础--数据类型
- 【邂逅Django】——(二)数据库配置
- [.NET6]使用ML.NET+ONNX预训练模型整活B站经典《华强买瓜》
猜你喜欢
![[MPC] ① quadratic programming problem matlab solver quadprog](/img/be/5e300255041e3348b933bc32e2ea46.png)
[MPC] ① quadratic programming problem matlab solver quadprog

Simulink simulation circuit model of open loop buck buck buck chopper circuit based on MATLAB

. Net 5.0+ does not need to rely on third-party native implementation of scheduled tasks
![[matytype] insert MathType inter line and intra line formulas in CSDN blog](/img/ff/871a3f06f898ed107a2a974d2c7bc4.png)
[matytype] insert MathType inter line and intra line formulas in CSDN blog

【MPC】①二次规划问题MATLAB求解器quadprog
![[.net6] use ml.net+onnx pre training model to liven the classic](/img/b3/b117481fba7257453011e4cdb1eaaa.png)
[.net6] use ml.net+onnx pre training model to liven the classic "Huaqiang buys melons" in station B

华为HMS Core携手超图为三维GIS注入新动能

网站源码整站下载 网站模板源代码下载

机器学习之线性回归详解
![[encounter Django] - (II) database configuration](/img/23/aed472757f7e238a146b043c0405d7.png)
[encounter Django] - (II) database configuration
随机推荐
CRC check
Is it safe to buy funds on the access letter?
JS基础--数据类型
Programmers want to go to state-owned enterprises? The technology is backward and the salary is low. I can't find a job after lying flat for several years
Have you learned the necessary global exception handler for the project
Matplotlib数据可视化基础
华为HMS Core携手超图为三维GIS注入新动能
SQL optimization - in and not in, exist
基于Matlab的开环Buck降压斩波电路Simulink仿真电路模型搭建
Dotnet console uses microsoft Maui. Getting started with graphics and skia
Suggest collecting | what to do when encountering slow SQL on opengauss?
[.NET6]使用ML.NET+ONNX预训练模型整活B站经典《华强买瓜》
A new round of popularity of digital collections opens
Simulink simulation circuit model of open loop buck buck buck chopper circuit based on MATLAB
[MPC] ① quadratic programming problem matlab solver quadprog
我国蜂窝物联网用户已达 15.9 亿,年内有望超越移动电话用户
Is the securities account opened by Yixue school for individuals safe? Is there a routine
内存泄漏定位工具之 valgrind 使用
IDEA运行报错Command line is too long. Shorten command line for...
商城小程序源码开源版-可二开