从 JSON 上的键和数组中提取值以“[”开头

Extract Values from key and array on JSON begins with '['

抱歉,我在网站上搜索了这个问题 但是我拥有的 Json 不同于网站上的任何其他因为它以 '['

开头

我厌倦了尝试提取值 然后我如何从 JSON 上的键和数组中提取值以 '['

开头
[{"date":"12/11/1990","name":"Delphi7"},{"date":"03/05/2012","name":"Delphi 10.4"}]

或提取表格:

[{"User":{"date":"12/11/1990","name":"Delphi7"}},{"User":{"date":"03/05/2012","name":"Delphi 10.4"}}]

在JSON中,[]表示一个数组。在这种情况下,您提供的两个示例都代表一个对象数组。

如果您使用 TJSONObject.ParseJSONValue(), it will return a TJSONValue pointer to a TJSONArray object that is holding TJSONObject 元素解析这些字符串,例如:

uses
  ..., System.JSON;

var
  JSONStr, DateStr, NameStr: string;
  JSONVal: TJSONValue;
  JSONArr: TJSONArray;
  JSONObj: TJSONObject;
  I: Integer;
begin
  JSONStr := '[{"date":"12/11/1990","name":"Delphi7"},{"date":"03/05/2012","name":"Delphi 10.4"}]';
  JSONVal := TJSONObject.ParseJSONValue(JSONStr);
  try
    JSONArr := JSONVal as TJSONArray;
    for I := 0 to JSONArr.Count-1 do
    begin
      JSONObj := JSONArr[I] as TJSONObject;
      DateStr := JSONObj.GetValue('date').Value;
      NameStr := JSONObj.GetValue('name').Value;
      ...
    end;
  finally
    JSONVal.Free;
  end;
end;
uses
  ..., System.JSON;

var
  JSONStr, DateStr, NameStr: string;
  JSONVal: TJSONValue;
  JSONArr: TJSONArray;
  JSONObj, JSONUser: TJSONObject;
  I: Integer;
begin
  JSONStr := '[{"User":{"date":"12/11/1990","name":"Delphi7"}},{"User":{"date":"03/05/2012","name":"Delphi 10.4"}}]';
  JSONVal := TJSONObject.ParseJSONValue(JSONStr);
  try
    JSONArr := JSONVal as TJSONArray;
    for I := 0 to JSONArr.Count-1 do
    begin
      JSONObj := JSONArr[I] as TJSONObject;
      JSONUser := JSONObj.GetValue('User') as TJSONObject;
      DateStr := JSONUser.GetValue('date').Value;
      NameStr := JSONUser.GetValue('name').Value;
      ...
    end;
  finally
    JSONVal.Free;
  end;
end;