Einzelnen Beitrag anzeigen

Blup

Registriert seit: 7. Aug 2008
Ort: Brandenburg
1.429 Beiträge
 
Delphi 10.4 Sydney
 
#7

AW: JSON in TFDQuery

  Alt 12. Jun 2023, 10:44
Der Code müsste beim compilieren eigentlich Meldungen über nicht initialisierte Variablen verursachen.
Sind diese Warnungen etwa ausgeschalten?

Es ist sinnvoll GetNextJSValue aufzuteilen und sinnvolle Namen für Variablen zu verwenden:
Delphi-Quellcode:
function GetFieldValue(AObject: TJSONObject; const AFieldname: string): TJSonValue;
begin
  if Assigned(AObject) and (AFieldname <> '') then
    Result := AObject.Values[Feld]
  else
    Result := nil;
end;

function GetSubObject(AObject: TJSONObject; const AFieldname: string): TJSONObject;
var
  lValue: TJSONValue;
begin
  lValue := GetFieldValue(AObject, AFieldname);
  if Assigned(lValue) and lValue is TJSONObject) then
    Result := TJSONObject(lValue)
  else
    Result := nil;
end;

function GetSubArray(AObject: TJSONObject; const AFieldname: string): TJSONArray;
var
  lValue: TJSONValue;
begin
  lValue := GetFieldValue(AObject, AFieldname);
  if Assigned(lValue) and lValue is TJSONArray) then
    Result := TJSONArray(lValue)
  else
    Result := nil;
end;
Delphi-Quellcode:
   Get_FirstJSValue('data', StreamString, obj_data, FirstJSValue);
   Try
     obj_device := GetSubObject(obj_data, 'device');
     obj_deviceStatus := GetSubObject(obj_device, 'deviceStatus');
     obj_workstep := GetSubObject(obj_deviceStatus, 'workstep');
     obj_job := GetSubObject(obj_workstep, 'job');
     obj_jobCustomer := GetSubObject(obj_job, 'jobCustomer');

     arr_employees := GetSubArray(obj_deviceStatus, 'employees');
     if Assigned(arr_employees) then
     begin
  Mit Zitat antworten Zitat