Oracle and JSON: Using PL/JSON
JSON (JavaScript. Object Notation) is a lightweight data format that is very well suited for transmitting data over the Internet. Despite the reference to JavaScript. in its name, JSON is a language-independent syntax and native support for it has been included in many modern programming languages. In fact, JSON is so popular nowadays that entire database management systems have built their record structure around it, for example Apache CouchDB .
As a Web developer who does a lot of work with PL/SQL, I have recently found myself creating PL/SQL procedures that output JSON data, and making these procedures available as RESTful HTTP services that are then consumed by Ajax functions in the front-end of my applications. This can be a bit of a pain, however, as I need to output the JSON data manually, making it very prone to errors and very difficult to debug in many cases.
Thankfully, an open source library for PL/SQL called PL/JSON is available that resolves some of the issues associated with working with JSON data in an Oracle application. In this post, you will learn how to install and use PL/JSON to work with JSON data in your own PL/SQL applications.
Download and install the library
The latest version of PL/JSON, at the time of writing, is version 0.8.6. The compressed archive is less than 200KB in size. You can download PL/JSON from SourceForge at http://sourceforge.net/projects/pljson/files/ . Unzip the library to your hard drive and install it in a SQL*Plus session using the following command:
You should see the following output:
----------------------------------- |
-- Compiling objects for PL/JSON -- |
----------------------------------- |
PL/SQL procedure successfully complete |
Assuming you see no errors from running the install script, PL/JSON is now installed in your Oracle database.
Using PL/JSON
With PL/JSON installed, you can now start working with the traditional “Hello, world” example, using the following PL/SQL code:
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/11893231/viewspace-672462/,如需转载,请注明出处,否则将追究法律责任。