Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.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
如何使用delphi从json中获取值_Delphi - Fatal编程技术网

如何使用delphi从json中获取值

如何使用delphi从json中获取值,delphi,Delphi,我有这个json,我想得到fname值。 我怎样才能用Delphi做到这一点 { "root":[ { "customers":[ { "fname":"George Makris", "Age":12 } ] } ] } 这就是我现在正在做的,但我认为这不是正确的方法 procedure TFo

我有这个json,我想得到fname值。 我怎样才能用Delphi做到这一点

{  
   "root":[  
      {  
         "customers":[  
            {  
               "fname":"George Makris",
               "Age":12
            }
         ]
      }
   ]
}
这就是我现在正在做的,但我认为这不是正确的方法

procedure TForm1.Button1Click(Sender: TObject);
  var s,json:string;
      myObj:TJSONObject;
      myarr:TJSONArray;
begin

json:='{"root":[{"customers":[ { "fname":"George Makris","Age":12}]}]}';
myObj := TJSONObject.ParseJSONValue(json) as TJSONObject;
myarr := myObj.GetValue('root') as TJSONArray;
myObj := myarr.Items[0] as TJSONObject;
myarr := myObj.GetValue('customers') as TJSONArray;
myObj := myarr.Items[0] as TJSONObject;
s := myObj.GetValue('fname').value;
showmessage(s);
end;

您的示例很接近,但会泄漏内存,特别是ParseJSONValue的结果

我更喜欢使用TryGetValue来验证内容是否存在。它还通过使用的参数推断类型。这里有一个两者都没有泄漏的例子

procedure TForm3.btnStartClick(Sender: TObject);
var
  s, JSON: string;
  jo: TJSONObject;
  myarr: TJSONArray;
begin
  JSON := '{"root":[{"customers":[ { "fname":"George Makris","Age":12}]}]}';
  jo := TJSONObject.ParseJSONValue(JSON) as TJSONObject;
  try
    if jo.TryGetValue('root', myarr) and (myarr.Count > 0) then
      if myarr.Items[0].TryGetValue('customers', myarr) and (myarr.Count > 0) then
        if myarr.Items[0].TryGetValue('fname', s) then
          showmessage(s);
  finally
    jo.Free;
  end;
end;

您的示例很接近,但会泄漏内存,特别是ParseJSONValue的结果

我更喜欢使用TryGetValue来验证内容是否存在。它还通过使用的参数推断类型。这里有一个两者都没有泄漏的例子

procedure TForm3.btnStartClick(Sender: TObject);
var
  s, JSON: string;
  jo: TJSONObject;
  myarr: TJSONArray;
begin
  JSON := '{"root":[{"customers":[ { "fname":"George Makris","Age":12}]}]}';
  jo := TJSONObject.ParseJSONValue(JSON) as TJSONObject;
  try
    if jo.TryGetValue('root', myarr) and (myarr.Count > 0) then
      if myarr.Items[0].TryGetValue('customers', myarr) and (myarr.Count > 0) then
        if myarr.Items[0].TryGetValue('fname', s) then
          showmessage(s);
  finally
    jo.Free;
  end;
end;

th-1的作用是什么?我没有投反对票,但可能是因为你没有清楚地描述你遇到的问题,比如编译器错误、运行时错误、返回的值不正确等等。从SO帮助中,我问了一些问题,寻求调试帮助,为什么这段代码不起作用?必须包括所需的行为、特定的问题或错误以及在问题本身中重现这些问题所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。我明白了,我必须阅读规则。好的,谢谢你的明确回答。th-1的作用是什么?我没有投反对票,但可能是因为你没有清楚描述你遇到的问题,即编译错误、运行时错误、返回的值不正确,等等。从SO帮助,寻求调试帮助的问题,为什么这段代码不起作用?必须包括所需的行为、特定的问题或错误以及在问题本身中重现这些问题所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。我明白了,我必须阅读规则。好的,谢谢你明确的回答,这就是为什么我问了,我回答了-1:这就是为什么我问了,我回答了-1: