JSON 来自 URL 的数组 DELPHI
JSON Array From URL in DELPHI
我有以下 Json 来自 url link 的数组。
[{"DATE":"01/10/2021","QUANTITY":4,"UNITPRICE":23.9},
{"DATE":"01/10/2021","QUANTITY":5.85,"UNITPRICE":23.9},
{"DATE":"30/09/2021","QUANTITY":10,"UNITPRICE":23.9},
{"DATE":"04/08/2021","QUANTITY":10,"UNITPRICE":28.83},
{"DATE":"01/07/2021","QUANTITY":1,"UNITPRICE":2.06},
{"DATE":"01/07/2021","QUANTITY":1,"UNITPRICE":2.06},
{"DATE":"30/06/2021","QUANTITY":1,"UNITPRICE":2.06},
{"DATE":"30/06/2021","QUANTITY":1,"UNITPRICE":2.06}]
我想从上面的 JSON 结构中提取数据,然后在 Memo 或 ListView 中显示它。
创建一个 Json 数组并将数据放入其中是否正确?当我 运行 我的代码并单击按钮时,它没有显示任何内容。另外,我是 Delphi.
的新手
这是我写的代码:
function GetURLAsString(const aurl: string): string;
var
IdHTTP1 : TidHTTP;
begin
IdHTTP1 := TIdHTTP.Create(nil);
try
IdHTTP1.IOHandler := TidSSLIOHandlerSocketOpenSSL.Create(IdHTTP1);
Result := IdHTTP1.Get(aurl);
finally
IdHTTP1.Free;
end;
end;
procedure TForm4.Button1Click(Sender: TObject);
var
jso : TJsonObject;
js : TJsonObject;
jsv : TJsonValue;
jsa : TJsonArray;
jsp : TJsonPair;
data : string;
i : integer;
LItem : TListViewItem;
begin
try
data := GetURLAsString('http://....');
except
on E: exception do
end;
try
jsv := TJSONObject.ParseJSONValue(data);
try
jso := jsv as TJSONObject;
jsa := TJSONArray.Create();
jsp := TJSONPair.Create('Array', jsa);
js.AddPair(jsp);
jsp := jso.Get('Array');
jsa := jsp.JsonValue as TJsonArray;
for I := 0 to jsa.Size - 1 do
begin
jso := jsa.Get(i) as TJsonObject;
for jsp in jso do
begin
if jsp.JsonString.Value = 'DATE' then
begin
form4.ListView1.BeginUpdate;
LItem := form4.ListView1.Items.Add;
LItem.Text := jso.GetValue('DATE').ToString;
form4.ListView1.EndUpdate;
end;
end;
end;
finally
jsv.Free;
end;
except
on E: exception do
end;
end;
我怀疑你会遇到访问冲突,因为你访问了未初始化的js变量的AddPair方法,由于你捕获了异常并忽略了它,所以你没有看到它。
但是,您使解决方案过于复杂,这会导致错误。
这应该有效:
jsa := jsv as TJSONArray;
form4.ListView1.BeginUpdate;
try
for I := 0 to jsa.Size - 1 do
begin
jso := jsa.Get(i) as TJsonObject;
LItem := form4.ListView1.Items.Add;
LItem.Text := jso.GetValue('DATE').ToString;
end;
finally
form4.ListView1.EndUpdate;
end;
finally
jsv.Free;
end;
我有以下 Json 来自 url link 的数组。
[{"DATE":"01/10/2021","QUANTITY":4,"UNITPRICE":23.9},
{"DATE":"01/10/2021","QUANTITY":5.85,"UNITPRICE":23.9},
{"DATE":"30/09/2021","QUANTITY":10,"UNITPRICE":23.9},
{"DATE":"04/08/2021","QUANTITY":10,"UNITPRICE":28.83},
{"DATE":"01/07/2021","QUANTITY":1,"UNITPRICE":2.06},
{"DATE":"01/07/2021","QUANTITY":1,"UNITPRICE":2.06},
{"DATE":"30/06/2021","QUANTITY":1,"UNITPRICE":2.06},
{"DATE":"30/06/2021","QUANTITY":1,"UNITPRICE":2.06}]
我想从上面的 JSON 结构中提取数据,然后在 Memo 或 ListView 中显示它。
创建一个 Json 数组并将数据放入其中是否正确?当我 运行 我的代码并单击按钮时,它没有显示任何内容。另外,我是 Delphi.
的新手这是我写的代码:
function GetURLAsString(const aurl: string): string;
var
IdHTTP1 : TidHTTP;
begin
IdHTTP1 := TIdHTTP.Create(nil);
try
IdHTTP1.IOHandler := TidSSLIOHandlerSocketOpenSSL.Create(IdHTTP1);
Result := IdHTTP1.Get(aurl);
finally
IdHTTP1.Free;
end;
end;
procedure TForm4.Button1Click(Sender: TObject);
var
jso : TJsonObject;
js : TJsonObject;
jsv : TJsonValue;
jsa : TJsonArray;
jsp : TJsonPair;
data : string;
i : integer;
LItem : TListViewItem;
begin
try
data := GetURLAsString('http://....');
except
on E: exception do
end;
try
jsv := TJSONObject.ParseJSONValue(data);
try
jso := jsv as TJSONObject;
jsa := TJSONArray.Create();
jsp := TJSONPair.Create('Array', jsa);
js.AddPair(jsp);
jsp := jso.Get('Array');
jsa := jsp.JsonValue as TJsonArray;
for I := 0 to jsa.Size - 1 do
begin
jso := jsa.Get(i) as TJsonObject;
for jsp in jso do
begin
if jsp.JsonString.Value = 'DATE' then
begin
form4.ListView1.BeginUpdate;
LItem := form4.ListView1.Items.Add;
LItem.Text := jso.GetValue('DATE').ToString;
form4.ListView1.EndUpdate;
end;
end;
end;
finally
jsv.Free;
end;
except
on E: exception do
end;
end;
我怀疑你会遇到访问冲突,因为你访问了未初始化的js变量的AddPair方法,由于你捕获了异常并忽略了它,所以你没有看到它。
但是,您使解决方案过于复杂,这会导致错误。 这应该有效:
jsa := jsv as TJSONArray;
form4.ListView1.BeginUpdate;
try
for I := 0 to jsa.Size - 1 do
begin
jso := jsa.Get(i) as TJsonObject;
LItem := form4.ListView1.Items.Add;
LItem.Text := jso.GetValue('DATE').ToString;
end;
finally
form4.ListView1.EndUpdate;
end;
finally
jsv.Free;
end;