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 |
AW: ParseJSONVALUE wie geht das?
So z.b.:
Delphi-Quellcode:
var s := TJSONObject(TJSONObject.ParseJSONValue(<Dein JSON string>)).P['data.translations[0].translatedText'].Value;
|
AW: ParseJSONVALUE wie geht das?
SUPER ich PRobiere es gleich aus.
Besten DANK. mfg ws |
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. |
AW: ParseJSONVALUE wie geht das?
Es könnte sein, dass der Pfad (P[]) in Delphi Berlin noch nicht unterstützt wird.
|
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;
|
AW: ParseJSONVALUE wie geht das?
Wie kann ich das in BeRLIN realisieren?
|
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:
Hier die Klasseuses 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;
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. |
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; |
AW: ParseJSONVALUE wie geht das?
Mit Delphi Berlin weiß ich nicht. Mit mORMot und Delphi Berlin so:
Delphi-Quellcode:
In mORMot gibt es noch mehr Möglichkeiten.
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)); Bis bald... Thomas |
Alle Zeitangaben in WEZ +1. Es ist jetzt 22:39 Uhr. |
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