Einzelnen Beitrag anzeigen

Ykcim

Registriert seit: 29. Dez 2006
Ort: NRW
824 Beiträge
 
Delphi 10.4 Sydney
 
#6

AW: JSON in TFDQuery

  Alt 12. Jun 2023, 08:39
Hallo Zusammen,

Rückmeldung, wie ich das Thema vorerst angegangen bin:

Nachdem es keine "einfache" Lösung gab, habe ich das JSON manuell ausgelesen:

Delphi-Quellcode:
procedure TForm1.Get_NextJSValue(Feld: string; ActJSValue: TJSONValue; var JSObj: TJSONObject; var JSValue: TJSONValue);
begin
   JSObj := TJSONObject(ActJSValue);
   if Feld <> 'then
      JSValue := JSObj.Values[Feld];
end;

function TForm1.MyStreamToString(aStream: TStream): widestring;
var    SS: TStringStream;
begin
   if aStream <> nil then begin
      Result := '{"data":';
      SS := TStringStream.Create('');
      try
         SS.CopyFrom(aStream, 0);
         Result := Result + SS.DataString;
         Result := Result + '}';
      finally
         SS.Free;
      end;
   end else begin
      Result := '';
   end;
end;
Delphi-Quellcode:
var FirstJSObject: TJSONObject;
      SecondJSObject: TJSONObject;
      ThirdJSObject: TJSONObject;
      FourthJSObject: TJSONObject;
      FivthJSObject: TJSONObject;
      SixthJSObject: TJSONObject;
      SeventhJSObject: TJSONObject;
      EigthJSObject: TJSONObject;
      FirstArr: TJSONArray;
      FirstJSValue: TJSONValue;
      SecondJSValue: TJSONValue;
      ThirdJSValue: TJSONValue;
      FourthJSValue: TJSONValue;
      FivthJSValue: TJSONValue;
      SixthJSValue: TJSONValue;
      SeventhJSValue: TJSONValue;
      EigthJSValue: TJSONValue;
      StreamString: widestring;
      j: integer;
begin

   StreamString := Get_MData(edt_URL.Text, true);
   Get_FirstJSValue('data', StreamString, FirstJSObject, FirstJSValue);
   Try
      if Assigned(FirstJSValue) and (FirstJSValue is TJSONObject) then begin
         Get_NextJSValue('device', FirstJSValue, SecondJSObject, SecondJSValue);

         if Assigned(SecondJSValue) and (SecondJSValue is TJSONObject) then begin
            Get_NextJSValue('deviceStatus', SecondJSValue, ThirdJSObject, ThirdJSValue);

            if Assigned(ThirdJSValue) and (ThirdJSValue is TJSONObject) then begin
               Get_NextJSValue('workstep', ThirdJSValue, FourthJSObject, FourthJSValue);

               if Assigned(FourthJSValue) and (FourthJSValue is TJSONObject) then begin
                  Get_NextJSValue('job', FourthJSValue, FivthJSObject, FivthJSValue);

                  if Assigned(FivthJSValue) and (FivthJSValue is TJSONObject) then begin
                     Get_NextJSValue('jobCustomer', FivthJSValue, SixthJSObject, SixthJSValue);

                     if Assigned(SixthJSValue) and (SixthJSValue is TJSONObject) then begin
                        Get_NextJSValue('', FivthJSValue, SeventhJSObject, SeventhJSValue);

                     end;
                  end;
               end;

               Get_NextJSValue('employees', ThirdJSValue, FourthJSObject, FourthJSValue);
               if Assigned(FourthJSValue) and (FourthJSValue is TJSONArray) then begin //JSONARRAY
                  FirstArr := TJSONArray(FourthJSValue);
                  for j := 0 to FirstArr.Count - 1 do begin
                     if FirstArr.Items[j] is TJSONObject then begin
                        EigthJSObject := TJSONObject(FirstArr.Items[j]);

                     end;
                  end;
               end;
            end;
         end;
      end;
      //Daten von Objekt abfragen
      ExtracJSValues(FirstJSObject, SecondJSObject, ThirdJSObject, FourthJSObject, FivthJSObject, SixthJSObject, SeventhJSObject, EigthJSObject, nil);
   finally
      FirstJSObject.Free;
      {SecondJSObject.Free;      //Hier gibt es eine Zugriffs-Exception
      ThirdJSObject.Free;
      FourthJSObject.Free;
      FivthJSObject.Free;
      SixthJSObject.Free;
      SeventhJSObject.Free;
      EigthJSObject.Free;}

   end;
end;
Delphi-Quellcode:
rocedure TForm1.ExtracJSValues(JSObj_1, JSObj_2, JSObj_3, JSObj_4, JSObj_5,
  JSObj_6, JSObj_7, JSObj_8: TJSONObject; Qry: TFDQuery);
var JOB_Bez, JOB_Nr, JOB_Name: string;
     PLANNED_GOOD_MANOUNT: integer;
     GOOD_CYCLES: integer;
     Speed: integer;
     PERCENT_COMPLETED: string;
     Start: string;
     Dauer: string;
     OperatorName: string;
     Operator_VName: string;
     Operator_NName: string;
     OPERATION_NAME: string;
     WORK_STEP_NAME: string;
     OPERATION_KEY: string;
begin
   if not JSObj_4.TryGetValue<string>('activity', OPERATION_NAME) then
      OPERATION_NAME := '';
   if not JSObj_4.TryGetValue<integer>('speed', Speed) then
      Speed := 0;

   if not JSObj_5.TryGetValue<string>('name', WORK_STEP_NAME) then
      WORK_STEP_NAME := '';
   if not JSObj_5.TryGetValue<string>('status', OPERATION_KEY) then
      OPERATION_KEY := '';

   if not JSObj_5.TryGetValue<string>('start', Start) then
      Start := '';

   if not JSObj_6.TryGetValue<string>('name', JOB_Bez) then
      JOB_Bez := '';
   if not JSObj_6.TryGetValue<string>('id', JOB_Nr) then
      JOB_Nr := '';
   JOB_Name := JOB_Nr + ' ' + JOB_Bez;

   if not JSObj_8.TryGetValue<string>('firstName', Operator_VName) then
      Operator_VName := '';
   if not JSObj_8.TryGetValue<string>('name', Operator_NName) then
      Operator_NName := '';
   OperatorName := Operator_VName + ' ' + Operator_NName;

   if assigned(Qry) then begin
      Qry.Insert;

   end;
end;
So weit bekomme ich die gewünschten Daten aus dem JSON raus. Jetzt muss ich die noch in geeigneter Form bereitstellen...

Herzlichen Gruß und allen eine gute Woche
Patrick
Patrick
  Mit Zitat antworten Zitat