delphi 用 System.JSON 解析 jsonarray

delphi parse jsonarray with System.JSON

使用 Delphi XE8 的 System.JSON,我无法解析 jsonarray

样本Json:

{
    "data":{
        "current_condition":[
            {
                "cloudcover":"0",
                "FeelsLikeC":"-9",
                "FeelsLikeF":"15",
                "humidity":"93",
                "observation_time":"04:10 AM",
                "precipMM":"0.0",
                "pressure":"1007",
                "temp_C":"-6",
                "temp_F":"21",
                "visibility":"10",
                "weatherCode":"113",
                "weatherDesc":[
                    
                ],
                "weatherIconUrl":[
                    
                ],
                "winddir16Point":"SE",
                "winddirDegree":"130",
                "windspeedKmph":"7",
                "windspeedMiles":"4"
            }
        ]
    }
}

使用代码:

memores: TStringList;
currcond: TJSONObject;

memores2.Text := http.Get ('url');
JSONObject := TJSONObject.ParseJSONValue(memores2.Text) as TJSONObject;
Memores1.add('current_condition');
JSONObject2 := JSONObject.GetValue('data')   as TJSONObject;
arrayjson := JSONObject2.ParseJSONValue('current_condition')  as TJSONArray;
currcond := arrayjson.Items[0] as TJSONObject;
memores1.Add((currcond.GetValue('cloudcover').Value));
arrayjson:=JSONObject2.ParseJSONValue('current_condition')  as TJSONArray;

这会尝试解析文本

current_condition

好像是 JSON。它不是。因此 arrayjsonnil,因此会产生运行时错误。将该行代码替换为:

arrayjson := JSONObject2.GetValue('current_condition') as TJSONArray;

例如这个程序:

{$APPTYPE CONSOLE}

uses
  System.SysUtils,
  System.JSON;

const
  Text =
    '{ "data":{ "current_condition":[ { "cloudcover":"0", "FeelsLikeC":"-9", "FeelsLikeF":"15", "humidity":"93", ' +
    '"observation_time":"04:10 AM", "precipMM":"0.0", "pressure":"1007", "temp_C":"-6", "temp_F":"21", "visibility":"10", ' +
    '"weatherCode":"113", "weatherDesc":[], "weatherIconUrl":[], "winddir16Point":"SE", "winddirDegree":"130", "windspeedKmph":"7", "windspeedMiles":"4" } ] } }';

procedure Main;
var
  JSONObject, JSONObject2, currcond: TJSONObject;
  arrayjson: TJSONArray;
begin
  JSONObject := TJSONObject.ParseJSONValue(Text) as TJSONObject;
  JSONObject2 := JSONObject.GetValue('data') as TJSONObject;
  arrayjson := JSONObject2.GetValue('current_condition') as TJSONArray;
  currcond := arrayjson.Items[0] as TJSONObject;
  Writeln(currcond.GetValue('observation_time').Value);
end;

begin
  try
    Main;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
  Readln;
end.

产出

04:10 AM

在上面的简单代码中,我没有尝试添加错误检查。你应该这样做。

作为所提供解决方案的替代方案,但使用 mORMot:

{$APPTYPE CONSOLE}

uses
  SynCommons;

const
    Text =  '{ "data":{ "current_condition":[ { "cloudcover":"0", "FeelsLikeC":"-9", "FeelsLikeF":"15", "humidity":"93", ' +
            '"observation_time":"04:10 AM", "precipMM":"0.0", "pressure":"1007", "temp_C":"-6", "temp_F":"21", "visibility":"10", ' +
            '"weatherCode":"113", "weatherDesc":[], "weatherIconUrl":[], "winddir16Point":"SE", "winddirDegree":"130", "windspeedKmph":"7", "windspeedMiles":"4" } ] } }';

var Data , CurrentConditionArray : TDocVariantData;
    Current_Condition , Json : Variant;
begin
  Json := _Json(Text);
  // Alternative 1
  Current_Condition := Json.data.current_condition._(0);
  Assert( Current_Condition.observation_time = '04:10 AM' );

  // Alternative 2 slightly faster
  Data := TDocVariantData(Json).O['data']^;
  CurrentConditionArray := Data.A['current_condition']^;
  Current_Condition := CurrentConditionArray.Values[0];
  Assert( TDocVariantData(Current_Condition).Value['precipMM'] = '0.0' );
end.

这应该适用于 Delphi 7 到 10.4。请在 amazing documentation

中找到更多详细信息和替代方案