我有这样一个数组:
{
"Test":
[
0,
1,
2,
3,
4
]
}我使用的是GNATCOLL.JSON,但我没有看到任何函数来处理数组并执行类似的操作,例如:
integer = Test (2);发布于 2019-07-09 09:52:18
你可能想试试:
function Get (Val : JSON_Value; Field : UTF8_String) return JSON_Array然后
function Get (Arr : JSON_Array; Index : Positive) return JSON_Value然后
function Get (Val : JSON_Value; Field : UTF8_String) return Integer例如,运行程序:
main.adb
with Ada.Text_IO;
with Ada.Text_IO.Unbounded_IO;
with Ada.Strings.Unbounded;
with GNATCOLL.JSON;
procedure Main is
use Ada.Text_IO;
use Ada.Strings.Unbounded;
Input : Unbounded_String := Null_Unbounded_String;
begin
-- Read.
declare
use Ada.Text_IO.Unbounded_IO;
Fd : File_Type;
begin
Open (Fd, In_File, "./example.json");
while not End_Of_File (Fd) loop
Input := Input & Unbounded_String'(Get_Line (Fd));
end loop;
Close (fd);
end;
-- Process.
declare
use GNATCOLL.JSON;
Root : JSON_Value := Read (Input);
Test : JSON_Array := Root.Get ("Test");
begin
for I in 1 .. Length (Test) loop
Put_Line ("Array element :" & Integer'Image (Get (Test, I).Get));
end loop;
end;
end Main;使用
example.json
{
"Test":
[
0,
1,
2,
3,
4
]
}收益率
$ ./main
Array element : 0
Array element : 1
Array element : 2
Array element : 3
Array element : 4https://stackoverflow.com/questions/56948828
复制相似问题