fpjson
fpc官方帮助:https://wiki.freepascal.org/fcl-json
fcl-json 是一个JSON实现。
它包含以下单位:
- fpjson:实现 TJsonData 及其子项的基本单元,例如 TJsonObject
- jsonParser:实现 TJsonParser,在下面的From JsonViewer示例中使用
- jsonConf:实现了 TJsonConfig,它可以方便地从/向文件读取/写入应用程序数据
- jsonScanner:json 源码词法分析器
uses fpjson, jsonparser; procedure JSONTest; var jData : TJSONData; jObject : TJSONObject; jArray : TJSONArray; s : String; begin // this is only a minimal sampling of what can be done with this API // create from string jData := GetJSON('{"Fld1" : "Hello", "Fld2" : 42, "Colors" : ["Red", "Green", "Blue"]}'); // output as a flat string s := jData.AsJSON; // output as nicely formatted JSON s := jData.FormatJSON; // cast as TJSONObject to make access easier jObject := TJSONObject(jData); // retrieve value of Fld1 s := jObject.Get('Fld1'); // change value of Fld2 jObject.Integers['Fld2'] := 123; // retrieve the second color s := jData.FindPath('Colors[1]').AsString; // add a new element jObject.Add('Happy', True); // add a new sub-array jArray := TJSONArray.Create; jArray.Add('North'); jArray.Add('South'); jArray.Add('East'); jArray.Add('West'); jObject.Add('Directions', jArray); end;
uses jsonConf; procedure TfmMain.SaveOptionsPos; var c: TJSONConfig; begin c:= TJSONConfig.Create(Nil); try c.Filename:= GetAppPath(cFileHistory); c.SetValue('/dialog/max', WindowState=wsMaximized); if WindowState<>wsMaximized then begin c.SetValue('/dialog/posx', Left); c.SetValue('/dialog/posy', Top); c.SetValue('/dialog/sizex', Width); c.SetValue('/dialog/sizey', Height); end; finally c.Free; end; end; procedure TfmMain.LoadOptionsPos; var nLeft, nTop, nW, nH: Integer; c: TJSONConfig; begin c:= TJSONConfig.Create(Nil); try c.Filename:= GetAppPath(cFileHistory); nLeft:= c.GetValue('/dialog/posx', Left); nTop:= c.GetValue('/dialog/posy', Top); nW:= c.GetValue('/dialog/sizex', Width); nH:= c.GetValue('/dialog/sizey', Height); SetBounds(nLeft, nTop, nW, nH); if c.GetValue('/dialog/max', false) then WindowState:= wsMaximized; finally c.Free; end; end;