![]() |
JSON String
Hallo Leute,
ich habe folgende frage: Ich habe diesen JAson string: {"products":[ {"id":123,"title":"abc"}, {"id":567,"title":"def"}, {"id":789,"title":"ghi"} ]} Ist es nun irgendwie mögliche herauszufinden welche Kategorien ("id" und "title") in diesem String drin sind. Also ist es möglich den STring irgendwie einzulesen und dann werden mir die Kategorien wieder zurück gegeben. Vielen Dank Jens |
AW: JSON String
Ja klar. Erstell dir einfach eine Klasse die das Objekt abbildet. Wichtig in dem Zusammenhang ist, dass die Felder mit "F" anfangen. Dann kannst du einfach mit
Delphi-Quellcode:
den JSON String einlesen und in ein Objekt wandeln lassen.
TJson.JsonToObject<TMyClass>(JsonString)
Siehe auch ![]() |
AW: JSON String
Hey, danke für deine schnelle antwort. Hast du vllt nen kleines beispiel oder nen Programm auschnitt wie man das umsetzt?
Code:
So mache ich das zur zeit. Aber will halt das ID und title variable halten.
SLid:=TStringList.Create;
SLtitle:=TStringList.Create; JsonValue := TJSonObject.ParseJSONValue(RESTResponse1.Content); for i := 0 to JSonValue.InstanceSize-2 do begin SLid.Add(JsonValue.GetValue<string>('products['+ Inttostr(i) +'].id')); SLtitle.add(JSonValue.GetValue<string>('products['+ Inttostr(i) +'].title')); end; danke dir :D |
AW: JSON String
Hi, ich habe das jetzt nur mal schnell im Editor runtergetippt damit du eine Idee bekommst wie so etwas ablaufen kann. Kann natürlich gut sein, dass hier und dort noch ein kleinerer Fehler drin ist. Aber das solltest du dann recht schnell in den Griff bekommen.
Delphi-Quellcode:
PS: Deine Delphi Version im Profil wäre hilfreich. Die
type
TProduct = class private FId: Integer; FTitle: string; public property ID: Integer read FId write FId; property Title: string read FTitle write FTitle; end; TProductList = class private FProducts: TArray<TProduct>; public property Products: TArray<TProduct> read FProducts write FProducts; end; implementation function ConvertToObject(s: string): TProductList; begin try Result := TJson.JsonToObject<TProductList>(s); except on E: Exception do begin // Log exception here end; end; end;
Delphi-Quellcode:
Klasse gibt es nämlich erst in den neueren Delphi Versionen.
Rest.Json
|
AW: JSON String
Super, Vielen Danken ich probiers mal aus :D (Dehlphi Version: 10.4)
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 10:49 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz