簡體   English   中英

從 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()解析這些字符串,它將返回一個TJSONValue指針,指向持有TJSONObject元素的TJSONArray object,例如:

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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM