[Delphi 11.3] du bon usage de system.JSON
Bonjour,
Je bute sur une chose qui me semblait simple au premier abords, mais je ne trouve pas ou j'ai "merdu", si je pouvais bénéficier de votre regards expert pour me dire ou cela pèche, je vous en serait reconnaissant...
Je télécharge un fichier JSON, en et une foi en mémoire, je cherche a le parser pour en extraire tout les éléments qui le compose.
Contenu du JSON:
Code:
1 2
|
[{"Colonies":"Les colonies"},{"Creatures":"Les r\u00e9atures"},{"Geneticiens":"Les g\u00e9n\u00e9ticiens"},{"Lieux":"Les lieux"},{"Mercenaires":"les mercenaires"},{"Nations":"Les nations"},{"Navires":"Les navires"},{"Personnages":"Les personnages"},{"Pirates":"Les pirates"},{"Reperes":"Les rep\u00e8res"},{"Societes":"Les soci\u00e9t\u00e9s"},{"Surfaces":"La surface"},{"Villes":"Les villes"}] |
Et voici les bouts de codes:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
type
ASyncJSonEvent = procedure(const AValue: Integer; const AString: string) of object;
/////////////////////////////////////
procedure TForm1.Button2Click(Sender: TObject);
begin
GetJSonFile('http://www.atlaspolaris.com/restful/listes.json', ParseJson);
end;
procedure TForm1.GetJsonFile(AUrl: string; AAfficheData: ASyncJSonEvent);
begin
TThread.CreateAnonymousThread(
procedure
var
LData: string;
LStatus: Integer;
LClient: THTTPClient;
LReponse: IHTTPResponse;
begin
LData := EmptyStr;
LStatus := 0;
LClient := THTTPClient.Create;
try
LReponse := LClient.Get(AUrl);
LStatus := LReponse.StatusCode;
if LReponse.StatusCode = 200 then
LData := TJSONObject.ParseJSONValue(LReponse.ContentAsString).ToString
else
LData := LReponse.StatusText;
finally
LClient.Free;
end;
TThread.Synchronize(TThread.CurrentThread,
procedure
begin
AAfficheData(LStatus, LData);
end);
end)
.Start;
end;
procedure TForm1.ParseJson(const AStatus: integer; const AValue: string);
var
LValue: TJSONValue;
LObject: TJSONObject;
LPair: TJSONPair;
I: integer;
begin
if AStatus = 200 then
begin
LValue := TJSONObject.ParseJSONValue(AValue);
LObject := TJSONObject(LValue);
for i := 0 to pred(LObject.Count) do
begin
LPair := LObject.Pairs[I];
SynEdit1.Lines.Add(LPair.JsonString.Value);
SynEdit1.Lines.Add(LPair.JsonValue.ToString);
end;
end;
end; |
Cela plante a ces lignes la sans que je comprenne pourquoi, surement une érreur de ma part mais je ne vois pas ou:
Code:
1 2 3
|
SynEdit1.Lines.Add(LPair.JsonString.Value);
SynEdit1.Lines.Add(LPair.JsonValue.ToString); |
Une idée de l'erreur que j'ai commise ?