Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   ParseJSONVALUE wie geht das? (https://www.delphipraxis.net/211134-parsejsonvalue-wie-geht-das.html)

wschrabi 1. Aug 2022 14:34

ParseJSONVALUE wie geht das?
 
Liebe Leute,
ich bekomme von einer API (Google Api) dieses JSON String Element zurück, das ich gerne parsen möchte. Ich benötige den TEIL Translated TEXT.

{"data":{"translations":[{"translatedText":"Synthese und Bewertung der spektralen Eigenschaften von fluoreszierenden Sonden auf Basis des 7-(Diethylamino)cumarin-Rückgrats"}]}}

Wie kann ich das mit Delphi Berlin machen?
Es gibt da TJSONObject.ParseJSONVALUE.

Kann mir jemand bitte ein CODE DEMO zum zuweisen nur des Translated TEXT zeigen? (ich möchte es einem String zuweisen)
BEsten DANK
ws

Union 1. Aug 2022 15:03

AW: ParseJSONVALUE wie geht das?
 
So z.b.:

Delphi-Quellcode:
var s := TJSONObject(TJSONObject.ParseJSONValue(<Dein JSON string>)).P['data.translations[0].translatedText'].Value;

wschrabi 1. Aug 2022 15:11

AW: ParseJSONVALUE wie geht das?
 
SUPER ich PRobiere es gleich aus.
Besten DANK.
mfg
ws

wschrabi 1. Aug 2022 15:15

AW: ParseJSONVALUE wie geht das?
 
Liste der Anhänge anzeigen (Anzahl: 1)
geht leider nicht.
Er erkennt P nicht siehe bild.
Ich habe DELPHI BERLIN. nicht C.

Union 1. Aug 2022 15:18

AW: ParseJSONVALUE wie geht das?
 
Es könnte sein, dass der Pfad (P[]) in Delphi Berlin noch nicht unterstützt wird.

wschrabi 1. Aug 2022 15:21

AW: ParseJSONVALUE wie geht das?
 
ja denke ich auch.
und damit bekomm ich eine ZUgriffsverletzung.
Delphi-Quellcode:
//  Result:=TJSONObject(TJSONObject.ParseJSONValue(translatedtext)).GetValue('data.translations[0].translatedText').tostring;

wschrabi 1. Aug 2022 15:22

AW: ParseJSONVALUE wie geht das?
 
Wie kann ich das in BeRLIN realisieren?

LoZe 1. Aug 2022 15:28

AW: ParseJSONVALUE wie geht das?
 
mein Tipp mach ne Klasse und nutzt ihn aus der Klasse raus.
das hier Hilft dir:
https://jsontodelphi.com/

der Aufruf ist dann ist dann nur

Delphi-Quellcode:

uses RootUnit;

function GetText(pJson:string):string;
var
  Root: TRoot;
begin
  Root := TRoot.Create;
  try
    Root.AsJson := pJson;
    Result:= Root.Data.Translations.First.TranslatedText;
    //oder per Index
    //Result:= Root.Data.Translations.Items[0].TranslatedText;
  finally
    Root.Free;
  end;
end;
Hier die Klasse

Delphi-Quellcode:
unit RootUnit;

interface

uses
  Pkg.Json.DTO, System.Generics.Collections, REST.Json.Types;

{$M+}

type
  TTranslations = class;

  TTranslations = class
  private
    FTranslatedText: string;
  published
    property TranslatedText: string read FTranslatedText write FTranslatedText;
  end;
 
  TData = class(TJsonDTO)
  private
    [JSONName('translations'), JSONMarshalled(False)]
    FTranslationsArray: TArray<TTranslations>;
    [GenericListReflect]
    FTranslations: TObjectList<TTranslations>;
    function GetTranslations: TObjectList<TTranslations>;
  protected
    function GetAsJson: string; override;
  published
    property Translations: TObjectList<TTranslations> read GetTranslations;
  public
    destructor Destroy; override;
  end;
 
  TRoot = class(TJsonDTO)
  private
    FData: TData;
  published
    property Data: TData read FData;
  public
    constructor Create; override;
    destructor Destroy; override;
  end;
 
implementation

{ TData }

destructor TData.Destroy;
begin
  GetTranslations.Free;
  inherited;
end;

function TData.GetTranslations: TObjectList<TTranslations>;
begin
  Result := ObjectList<TTranslations>(FTranslations, FTranslationsArray);
end;

function TData.GetAsJson: string;
begin
  RefreshArray<TTranslations>(FTranslations, FTranslationsArray);
  Result := inherited;
end;

{ TRoot }

constructor TRoot.Create;
begin
  inherited;
  FData := TData.Create;
end;

destructor TRoot.Destroy;
begin
  FData.Free;
  inherited;
end;

end.

Union 1. Aug 2022 15:33

AW: ParseJSONVALUE wie geht das?
 
Vielleicht so (Destructor zum Aufräumen des Arrays müsste noch gemacht werden):

Delphi-Quellcode:
interface

uses
  REST.Json;


  TTranslation = class
  private
    FtranslatedText: string;
  public
    property translatedText: string read FtranslatedText write FtranslatedText;
  end;

  Ttranslations = TArray<TTranslation>;

  Tdata = class
  private
    Ftranslations: TTranslations;
  public
    property translations: TTranslations read Ftranslations write Ftranslations;
  end;

  TApi = class
  private
    Fdata: Tdata;
  public
    property data: Tdata read Fdata write Fdata;
  end;

implementation

var s := TJson.JsonToObject<TApi>('<Dein JSON-String>').data.translations[0].translatedText;

mytbo 1. Aug 2022 15:46

AW: ParseJSONVALUE wie geht das?
 
Mit Delphi Berlin weiß ich nicht. Mit mORMot und Delphi Berlin so:
Delphi-Quellcode:
uses
  mormot.core.base,
  mormot.core.json;
 
type
  TResultRec = packed record
    data: record
      translations: array of record
        translatedText: RawUtf8;
      end;
    end;
  end;
 
const
  JSON: RawUtf8 = '{"data":{"translations":[{"translatedText":"Synthese und Bewertung der spektralen Eigenschaften von fluoreszierenden Sonden auf Basis des 7-(Diethylamino)cumarin-Rückgrats"}]}}';
var
  rec: TResultRec;
begin
  LoadJson(rec, JSON, TypeInfo(TResultRec));
  ShowMessage(Utf8ToSTring(rec.data.translations[0].translatedText));
In mORMot gibt es noch mehr Möglichkeiten.

Bis bald...
Thomas


Alle Zeitangaben in WEZ +1. Es ist jetzt 22:39 Uhr.
Seite 1 von 2  1 2      

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz