使用jsonobject.count和pairs时出错
我想通过以下示例处理json字符串:使用jsonobject.count和pairs时出错,json,delphi,Json,Delphi,我想通过以下示例处理json字符串: {"colors":[{"name":"red","hex":"#f00"},{"name":"blue","hex":"#xxx"}]} 我尝试了不同的变体,但出现以下错误: Tjsonobject在第…行不包含名为“Count”的成员。 Tjsonobject在第…行不包含名为“Pairs”的成员。 未声明的标识符JsonString 我使用了DBXJson Delphi 10.2.3,vcl应用程序 代码是: var o: TJSONObject;
{"colors":[{"name":"red","hex":"#f00"},{"name":"blue","hex":"#xxx"}]}
我尝试了不同的变体,但出现以下错误:
Tjsonobject在第…行不包含名为“Count”的成员。Tjsonobject在第…行不包含名为“Pairs”的成员。
未声明的标识符JsonString 我使用了DBXJson
Delphi 10.2.3,vcl应用程序
代码是:
var
o: TJSONObject;
a: TJSONArray;
book: TJSONObject;
idx: integer;
idy: integer;
begin
o := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(Memo1.Lines.Text),0) as TJSONObject;
try
a := TJSONArray(o.Get('colors').JsonValue);
for idx := 0 to pred(a.size) do begin
book := TJSONObject(a.Get(idx));
for idy := 0 to pred(book.Count) do begin
ShowMessage( book.Pairs[idy].JsonString.ToString + ':' + book.Pairs[idy].JsonValue.ToString );
end;
end;
finally
o.Free;
end;
end;
JSON的新功能,用于学习,但我无法理解如果您正在学习如何在delphi中使用JSON,我建议您不要使用system.JSON,它太复杂且速度非常慢。请查看TalJsonDocument的
如果您正在学习如何在delphi中使用json,我建议您不要使用system.json,因为它太复杂而且速度很慢。请查看TalJsonDocument的
你的问题和评论相互矛盾。您的问题说您正在使用Delphi10.2.3,但您的评论说您正在使用XE5 在Delphi10.2.3中,使用
System.JSON
单元。在XE5中,使用Data.DBXJson
单元
如果阅读XE5版本的TJSONObject
,它没有Count
和Pairs
属性,因此在内部循环中会看到错误。您应该改为使用属性和方法,就像您的外部循环已经在使用一样,例如:
var
o: TJSONObject;
a: TJSONArray;
book: TJSONObject;
p: TJSONPair;
idx: integer;
idy: integer;
begin
o := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(Memo1.Lines.Text),0) as TJSONObject;
try
a := o.Get('colors').JsonValue as TJSONArray;
for idx := 0 to pred(a.Size) do
begin
book := a.Get(idx).JsonValue as TJSONObject;
for idy := 0 to pred(book.Size) do
begin
p := book.Get(idy);
ShowMessage(p.JsonString.ToString + ':' + p.JsonValue.ToString);
end;
end;
finally
o.Free;
end;
end;
或者,XE5和10.2.3中的TJSONArray
和TJSONObject
都支持Enumerator
,因此您可以使用for..in
循环,例如:
var
o: TJSONObject;
a: TJSONArray;
book: TJSONObject;
v: TJSONValue;
p: TJSONPair;
begin
o := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(Memo1.Lines.Text),0) as TJSONObject;
try
a := o.Get('colors').JsonValue as TJSONArray;
for v in a do
begin
book := v as TJSONObject;
for p in book do
begin
ShowMessage(p.JsonString.ToString + ':' + p.JsonValue.ToString);
end;
end;
finally
o.Free;
end;
end;
你的问题和评论相互矛盾。您的问题说您正在使用Delphi10.2.3,但您的评论说您正在使用XE5 在Delphi10.2.3中,使用
System.JSON
单元。在XE5中,使用Data.DBXJson
单元
如果阅读XE5版本的TJSONObject
,它没有Count
和Pairs
属性,因此在内部循环中会看到错误。您应该改为使用属性和方法,就像您的外部循环已经在使用一样,例如:
var
o: TJSONObject;
a: TJSONArray;
book: TJSONObject;
p: TJSONPair;
idx: integer;
idy: integer;
begin
o := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(Memo1.Lines.Text),0) as TJSONObject;
try
a := o.Get('colors').JsonValue as TJSONArray;
for idx := 0 to pred(a.Size) do
begin
book := a.Get(idx).JsonValue as TJSONObject;
for idy := 0 to pred(book.Size) do
begin
p := book.Get(idy);
ShowMessage(p.JsonString.ToString + ':' + p.JsonValue.ToString);
end;
end;
finally
o.Free;
end;
end;
或者,XE5和10.2.3中的TJSONArray
和TJSONObject
都支持Enumerator
,因此您可以使用for..in
循环,例如:
var
o: TJSONObject;
a: TJSONArray;
book: TJSONObject;
v: TJSONValue;
p: TJSONPair;
begin
o := TJSONObject.ParseJSONValue(TEncoding.ASCII.GetBytes(Memo1.Lines.Text),0) as TJSONObject;
try
a := o.Get('colors').JsonValue as TJSONArray;
for v in a do
begin
book := v as TJSONObject;
for p in book do
begin
ShowMessage(p.JsonString.ToString + ':' + p.JsonValue.ToString);
end;
end;
finally
o.Free;
end;
end;
将
DBXJson
替换为System.JSON
(或只是JSON
)。我得到另一个错误:在Delphi XE5上无法解析单元名称'System.JSON'或'JSON',在Delphi 10.2.3上通过重新安装Delphi解决了相同的错误,但是在XE5上,问题remainSystem.JSON在XE5 AFAIK中不存在。将DBXJson
替换为System.JSON
(或者只是JSON
)。我得到另一个错误:在Delphi XE5上无法解析单元名'System.JSON'或'JSON',我在Delphi 10.2.3上通过重新安装Delphi解决了相同的错误,但是在XE5上,问题remainsSystem.JSON在XE5 AFAIK.yes中不存在,并且示例数据。。。您将看到delphi中的Json速度非常慢(但实际上非常慢),TalJsonDoc的速度为3毫秒,而DBXJsonyes甚至内存不足,示例数据。。。您将看到delphi中的Json速度非常慢(但实际上非常慢),使用TalJsonDoc时,即使使用DBXJson时内存不足,也需要3毫秒