Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
prog:lazarus:classes:json [03/06/2020 19:00] thierry créée |
prog:lazarus:classes:json [01/05/2021 17:20] (Version actuelle) thierry [FindPath] |
||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
====== JSON ====== | ====== JSON ====== | ||
- | ====== Sources et Ressources ====== | + | ===== Créer un JSON ===== |
+ | ==== A partir d'un String ==== | ||
+ | |||
+ | <code delphi> | ||
+ | var | ||
+ | vJD: TJsonData; | ||
+ | begin | ||
+ | vJD := GetJSON(AString); | ||
+ | try | ||
+ | if assigned(vJD) then { Ok le JSON est valide } | ||
+ | begin | ||
+ | ... | ||
+ | end; | ||
+ | finally | ||
+ | vJD.free; | ||
+ | end; | ||
+ | end; | ||
+ | </code> | ||
+ | |||
+ | ===== Lire une valeur ===== | ||
+ | ==== TJSonObject.Get ==== | ||
+ | |||
+ | ''JSon.GET('Nom element', VALEUR PAR DEFAUT);''\\ | ||
+ | ou\\ | ||
+ | ''JSon.GET('Nom element');''... mais voir remarque ci-dessous ! | ||
+ | |||
+ | |||
+ | <code delphi> | ||
+ | var | ||
+ | vJO: TJSonObject; | ||
+ | begin | ||
+ | FBigEndian:=vJO.Get('bendian',false); | ||
+ | end; | ||
+ | </code> | ||
+ | Fonctionne sur : | ||
+ | <code delphi fpjson.pp> | ||
+ | Function Get(Const AName : String) : Variant; | ||
+ | Function Get(Const AName : String; ADefault : TJSONFloat) : TJSONFloat; | ||
+ | Function Get(Const AName : String; ADefault : Integer) : Integer; | ||
+ | Function Get(Const AName : String; ADefault : Int64) : Int64; | ||
+ | Function Get(Const AName : String; ADefault : QWord) : QWord; | ||
+ | Function Get(Const AName : String; ADefault : Boolean) : Boolean; | ||
+ | Function Get(Const AName : String; ADefault : TJSONStringType) : TJSONStringType; | ||
+ | Function Get(Const AName : String; ADefault : TJSONUnicodeStringType) : TJSONUnicodeStringType; | ||
+ | Function Get(Const AName : String; ADefault : TJSONArray) : TJSONArray; | ||
+ | Function Get(Const AName : String; ADefault : TJSONObject) : TJSONObject; | ||
+ | |||
+ | </code> | ||
+ | |||
+ | <note tip>Remarquez que chaque Function **Get** peut prendre un paramètre //ADefault//.\\ | ||
+ | C'est intéressant d'utiliser ce paramètre pour 3 raisons : | ||
+ | - Il évite le déclenchement d'une exception si aucun élément de nom //AName// n'est trouvé. | ||
+ | - Il définit une valeur que sera retournée si aucun élément de nom //AName// n'est trouvé. | ||
+ | - Il permet de savoir quel **Get** on utilisera en fonction de son type.\\ Cela evite d'appeler le **Get** générique qui passe par les Variants | ||
+ | </note> | ||
+ | ==== TJSonData.FindPath ==== | ||
+ | Pour récuperer le JSONArray ''version'' dans le JSON suivant : | ||
+ | <code javascript> | ||
+ | { | ||
+ | "json_format_version": [ | ||
+ | 1, | ||
+ | 0 | ||
+ | ], | ||
+ | "smartctl": { | ||
+ | "version": [ | ||
+ | 7, | ||
+ | 2 | ||
+ | ], | ||
+ | "svn_revision": "5155", | ||
+ | "build_info": "(sf-7.2-1)", | ||
+ | "exit_status": 0 | ||
+ | } | ||
+ | } | ||
+ | </code> | ||
+ | On peut utiliser le code ci-dessous (''TJsonData.FindPath''): | ||
+ | <code delphi> | ||
+ | vJSA:=TJSONArray(OutJSON.FindPath('smartctl.version')); | ||
+ | </code> | ||
+ | Pour avoir seulement le ''7'' : | ||
+ | <code delphi> | ||
+ | var | ||
+ | vJD: TJSonData; | ||
+ | ... | ||
+ | vJD:= OutJSON.FindPath('smartctl.version[0]'); | ||
+ | if assigned(vJD) then Result:=vJD.AsString; | ||
+ | </code> | ||
+ | ==== TJSonObject.Find (la sécurité) ==== | ||
+ | Dans l'exemple ci dessous on veut récupéré un eventuel JSonArray dans le JSonObject ''vJO''. | ||
+ | <code delphi> | ||
+ | var | ||
+ | vJO: TJSONObject; | ||
+ | vJA: TJSonArray; | ||
+ | vI: integer; | ||
+ | vS: string; | ||
+ | begin | ||
+ | ... | ||
+ | if vJO.Find('dependencies',vJA) then | ||
+ | for vI := 0 to vJA.Count - 1 do | ||
+ | vS:= vS+vJA.Strings[vI]; | ||
+ | ... | ||
+ | end; | ||
+ | </code> | ||
+ | ''TJsonObject.Find'' fonctionne avec tous ces types : | ||
+ | <code delphi> | ||
+ | Function Find(Const AName : String) : TJSONData; overload; | ||
+ | Function Find(Const AName : String; AType : TJSONType) : TJSONData; overload; | ||
+ | function Find(const key: TJSONStringType; out AValue: TJSONData): boolean; | ||
+ | function Find(const key: TJSONStringType; out AValue: TJSONObject): boolean; | ||
+ | function Find(const key: TJSONStringType; out AValue: TJSONArray): boolean; | ||
+ | function Find(const key: TJSONStringType; out AValue: TJSONString): boolean; | ||
+ | function Find(const key: TJSONStringType; out AValue: TJSONBoolean): boolean; | ||
+ | function Find(const key: TJSONStringType; out AValue: TJSONNumber): boolean; | ||
+ | |||
+ | </code> | ||
+ | |||
+ | |||
+ | ====== Sources & Ressources ====== | ||
* [[https://darrylsite.developpez.com/tutoriels/pascal/json/]] | * [[https://darrylsite.developpez.com/tutoriels/pascal/json/]] | ||
+ | * [[https://wiki.freepascal.org/fcl-json]] | ||
+ | * [[https://wiki.freepascal.org/Streaming_JSON|Loading and storing (streaming) objects (TObject -> JSON)]] | ||