Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用jsonobject.count和pairs时出错_Json_Delphi - Fatal编程技术网

使用jsonobject.count和pairs时出错

使用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;

我想通过以下示例处理json字符串:

{"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毫秒