如何使用 Ada 从 https 网站解析 JSON

How to parse JSON from https website using Ada

我正在尝试为 https 网站推送制作一个简单的解析器,因此尝试按照 http://rosettacode.org/wiki/HTTP#Ada 中的示例进行操作,只需更改网站的地址即可。

所以我尝试了

    with Ada.Text_IO; use Ada.Text_IO;
    with AWS.Client;
    with AWS.Response;
    procedure Main_Other is
    begin
       Put_Line (AWS.Response.Message_Body
                 (AWS.Client.Get
                    (URL => "https://google.com")));
    end Main_Other;

但是我有一个例外

raised PROGRAM_ERROR : aws-client.adb:398 finalize/adjust raised exception [2020-04-02 10:41:20] process exited with status 1, elapsed time: 00.80s

那么,有什么解决办法吗?

我想解析网站中某些表格的当前状态,类似于在 Python

中制作类似的东西
    import pandas as pd
    def retrieve_json(json_url):
        return pd.read_json(json_url)

我想尽可能以最简单的方式编写此解决方案,最好不要依赖 AWS。

拜托,谢谢。

您似乎正在使用禁用了 SSL 支持的 AWS 版本(SSL 支持是可选的)。将 https 替换为 http 应该可以解决问题。如果不安全的连接不是一个选项,要么在启用 SSL 支持的情况下重新编译 AWS(请参阅 here and here),或者,如果您赶时间并且碰巧在 Linux 上开发,请回到 [=15] =] 和 wget(使用 GNAT CE 2019 测试):

main.adb

with Ada.Text_IO;   use Ada.Text_IO;
with GNAT.Expect;   use GNAT.Expect; 
with GNATCOLL.JSON; use GNATCOLL.JSON;

procedure Main is

   -------------------
   -- Download_JSON --
   -------------------

   function Download_JSON (URL : String) return JSON_Value is      

      Cmd    : constant String := "wget";
      Arg_1  : aliased String := "-q";   --  Turn off Wget's own messages.
      Arg_2  : aliased String := "-O";   --  Output response to ...
      Arg_3  : aliased String := "-";    --     ... standard output.
      Arg_4  : aliased String := URL;
      Status : aliased Integer;

      Response : String :=
        Get_Command_Output
          (Command    => Cmd,
           Arguments  => (1 => Arg_1'Unchecked_Access,
                          2 => Arg_2'Unchecked_Access,
                          3 => Arg_3'Unchecked_Access,
                          4 => Arg_4'Unchecked_Access),
           Input      => "",
           Status     => Status'Unchecked_Access);

   begin
      --  Omitting check of 'Status' for brevity.
      return Read (Response); 
   end;


   Root : JSON_Value;

begin

   Root := Download_JSON
     ("https://raw.githubusercontent.com/AdaCore/gnatcoll-core/" &
        "master/testsuite/tests/json/validation/basic_object.json");

   Put_Line (Write (Root));

end Main;

default.gpr

with "gnatcoll.gpr";

project Default is
    for Source_Dirs use ("src");
    for Object_Dir use "obj";
    for Main use ("main.adb");
end Default;

输出

$ ./main
{"a":1,"b":"a tringg","c":[1,2,3],"d":{"a":"a"},"e":null}