<i id='C596j'><tr id='C596j'><dt id='C596j'><q id='C596j'><span id='C596j'><b id='C596j'><form id='C596j'><ins id='C596j'></ins><ul id='C596j'></ul><sub id='C596j'></sub></form><legend id='C596j'></legend><bdo id='C596j'><pre id='C596j'><center id='C596j'></center></pre></bdo></b><th id='C596j'></th></span></q></dt></tr></i><div id='C596j'><tfoot id='C596j'></tfoot><dl id='C596j'><fieldset id='C596j'></fieldset></dl></div>
    1. <tfoot id='C596j'></tfoot>
        <bdo id='C596j'></bdo><ul id='C596j'></ul>

      <small id='C596j'></small><noframes id='C596j'>

      <legend id='C596j'><style id='C596j'><dir id='C596j'><q id='C596j'></q></dir></style></legend>

      在 oracle 中使用 json

      时间:2023-09-19

          <tbody id='xXPCl'></tbody>

        <i id='xXPCl'><tr id='xXPCl'><dt id='xXPCl'><q id='xXPCl'><span id='xXPCl'><b id='xXPCl'><form id='xXPCl'><ins id='xXPCl'></ins><ul id='xXPCl'></ul><sub id='xXPCl'></sub></form><legend id='xXPCl'></legend><bdo id='xXPCl'><pre id='xXPCl'><center id='xXPCl'></center></pre></bdo></b><th id='xXPCl'></th></span></q></dt></tr></i><div id='xXPCl'><tfoot id='xXPCl'></tfoot><dl id='xXPCl'><fieldset id='xXPCl'></fieldset></dl></div>
          <tfoot id='xXPCl'></tfoot>
          <legend id='xXPCl'><style id='xXPCl'><dir id='xXPCl'><q id='xXPCl'></q></dir></style></legend>
            <bdo id='xXPCl'></bdo><ul id='xXPCl'></ul>

              1. <small id='xXPCl'></small><noframes id='xXPCl'>

              2. 本文介绍了在 oracle 中使用 json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                有没有一种在 oracle 中使用 JSON 的简单方法?我有一个经常用来调用 Web 服务的标准过程,JSON 是我在 Web 开发环境中熟悉的一种格式,但是在存储过程中使用 json 的最佳方法是什么?例如,从 URI 中获取 CLOB 响应,将其转换为 JSON 对象并从中获取值?

                Is there an easy way to work with JSON within oracle? I have a standard procedure that I use to call web services quite often, JSON is a format that I am familiar with in web development context, but what is the best way to work with json within a stored procedure? For instance take the CLOB response from the URI, convert that to a JSON object and get a value from that?

                为了参考,这里是我用来获取 URL 的过程

                For reference sake, here is the procedure I used to fetch URLs

                create or replace procedure macp_URL_GET(url_resp in out clob, v_url in varchar2) is
                   req     Utl_Http.req;
                   resp    Utl_Http.resp;
                   NAME    VARCHAR2 (255);
                   VALUE   VARCHAR2 (1023);
                   v_msg   VARCHAR2 (80);
                   v_ans clob;
                --   v_url   VARCHAR2 (32767) := 'http://www.macalester.edu/';
                BEGIN
                   /* request that exceptions are raised for error Status Codes */
                   Utl_Http.set_response_error_check (ENABLE => TRUE );
                   /* allow testing for exceptions like Utl_Http.Http_Server_Error */
                   Utl_Http.set_detailed_excp_support (ENABLE => TRUE );
                   /*
                   Utl_Http.set_proxy (
                      proxy                 => 'www-proxy.us.oracle.com',
                      no_proxy_domains      => 'us.oracle.com'
                   );
                   */
                   req := Utl_Http.begin_request (url => v_url, method => 'GET');
                   /*
                    Alternatively use method => 'POST' and Utl_Http.Write_Text to
                    build an arbitrarily long message
                  */
                
                  /*
                   Utl_Http.set_authentication (
                      r              => req,
                      username       => 'SomeUser',
                      PASSWORD       => 'SomePassword',
                      scheme         => 'Basic',
                      for_proxy      => FALSE      --this info is for the target Web server 
                   );
                   */
                
                   Utl_Http.set_header (r => req, NAME => 'User-Agent', VALUE => 'Mozilla/4.0');
                   resp := Utl_Http.get_response (r => req);
                   /*
                   DBMS_OUTPUT.put_line ('Status code: ' || resp.status_code);
                   DBMS_OUTPUT.put_line ('Reason phrase: ' || resp.reason_phrase);
                   FOR i IN 1 .. Utl_Http.get_header_count (r => resp)
                   LOOP
                      Utl_Http.get_header (r => resp, n => i, NAME => NAME, VALUE => VALUE);
                      DBMS_OUTPUT.put_line (NAME || ': ' || VALUE);
                   END LOOP;
                   */
                --test
                   BEGIN
                      LOOP
                         Utl_Http.read_text (r => resp, DATA => v_msg);
                         --DBMS_OUTPUT.put_line (v_msg);
                         v_ans := v_ans || v_msg;
                         url_resp := url_resp || v_msg;
                      END LOOP;
                   EXCEPTION
                      WHEN Utl_Http.end_of_body
                      THEN
                         NULL;
                   END;
                --test
                   Utl_Http.end_response (r => resp);
                
                
                   --url_resp := v_ans;
                
                EXCEPTION
                   /*
                    The exception handling illustrates the use of "pragma-ed" exceptions
                    like Utl_Http.Http_Client_Error. In a realistic example, the program
                    would use these when it coded explicit recovery actions.
                
                    Request_Failed is raised for all exceptions after calling
                    Utl_Http.Set_Detailed_Excp_Support ( ENABLE=>FALSE )
                    And it is NEVER raised after calling with ENABLE=>TRUE
                  */
                   WHEN Utl_Http.request_failed
                   THEN
                      DBMS_OUTPUT.put_line (
                         'Request_Failed: ' || Utl_Http.get_detailed_sqlerrm
                      );
                      url_resp :='Request_Failed: ' || Utl_Http.get_detailed_sqlerrm;
                   /* raised by URL http://xxx.oracle.com/ */
                   WHEN Utl_Http.http_server_error
                   THEN
                      DBMS_OUTPUT.put_line (
                         'Http_Server_Error: ' || Utl_Http.get_detailed_sqlerrm
                      );
                      url_resp := 'Http_Server_Error: ' || Utl_Http.get_detailed_sqlerrm;
                   /* raised by URL http://otn.oracle.com/xxx */
                   WHEN Utl_Http.http_client_error
                   THEN
                      DBMS_OUTPUT.put_line (
                         'Http_Client_Error: ' || Utl_Http.get_detailed_sqlerrm
                      );
                      url_resp := 'Http_Client_Error: ' || Utl_Http.get_detailed_sqlerrm;
                   /* code for all the other defined exceptions you can recover from */
                   WHEN OTHERS
                   THEN
                      DBMS_OUTPUT.put_line (SQLERRM);
                      url_resp := SQLERRM;
                END;
                

                然后测试一下

                begin
                  macp_url_get(url_resp => :url_resp,
                               'http://maps.googleapis.com/maps/api/geocode/json?address=55105&sensor=false');
                end;
                

                (我知道 googleapi 将允许 xml 响应,但我经常使用其他默认为 JSON 的 Web API)

                (I know that the googleapi will allow xml response, but there are other web APIs that I use regularly that default to JSON)

                推荐答案

                我已经开始使用这个库,看起来很有希望:https://github.com/pljson/pljson

                I have started using this library, and it seems promising: https://github.com/pljson/pljson

                易于安装,示例很好.

                要在您的示例中使用库,请将这些变量添加到您的过程中..

                To use the library in your example, add these variables to your procedure..

                mapData     json;
                results     json_list;
                status      json_value;
                firstResult json;
                geometry    json;
                

                ....

                然后您可以将响应作为 json 对象进行操作.

                Then you can manipulate the response as a json object.

                -- convert the result from the get to a json object, and show some results.
                mapData := json(v_ans);
                
                -- Show the status of the request
                status := mapData.get('status');
                dbms_output.put_line('Status = ' || status.get_string());
                
                IF (status.get_string() = 'OK') THEN
                  results := json_list(mapData.get('results'));
                  -- Grab the first item in the list
                  resultObject := json(results.head);
                
                  -- Show the human readable address 
                  dbms_output.put_line('Address = ' || resultObject.get('formatted_address').to_char() );
                  -- Show the json location data 
                  dbms_output.put_line('Location = ' || resultObject.get('geometry').to_char() );
                END IF;
                

                运行此代码会将其输出到 dbms 输出:

                Running this code will output this to the dbms output:

                Status = OK
                Address = "St Paul, MN 55105, USA"
                Location = {
                  "bounds" : {
                    "northeast" : {
                      "lat" : 44.9483849,
                      "lng" : -93.1261959
                    },
                    "southwest" : {
                      "lat" : 44.9223829,
                      "lng" : -93.200307
                    }
                  },
                  "location" : {
                    "lat" : 44.9330076,
                    "lng" : -93.16290629999999
                  },
                  "location_type" : "APPROXIMATE",
                  "viewport" : {
                    "northeast" : {
                      "lat" : 44.9483849,
                      "lng" : -93.1261959
                    },
                    "southwest" : {
                      "lat" : 44.9223829,
                      "lng" : -93.200307
                    }
                  }
                }
                

                这篇关于在 oracle 中使用 json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何改变约束 下一篇:Oracle:如何将分钟添加到时间戳?

                相关文章

              3. <small id='cjxXX'></small><noframes id='cjxXX'>

              4. <i id='cjxXX'><tr id='cjxXX'><dt id='cjxXX'><q id='cjxXX'><span id='cjxXX'><b id='cjxXX'><form id='cjxXX'><ins id='cjxXX'></ins><ul id='cjxXX'></ul><sub id='cjxXX'></sub></form><legend id='cjxXX'></legend><bdo id='cjxXX'><pre id='cjxXX'><center id='cjxXX'></center></pre></bdo></b><th id='cjxXX'></th></span></q></dt></tr></i><div id='cjxXX'><tfoot id='cjxXX'></tfoot><dl id='cjxXX'><fieldset id='cjxXX'></fieldset></dl></div>

                    <bdo id='cjxXX'></bdo><ul id='cjxXX'></ul>

                  <tfoot id='cjxXX'></tfoot>

                  <legend id='cjxXX'><style id='cjxXX'><dir id='cjxXX'><q id='cjxXX'></q></dir></style></legend>