DFJsonLib is a Delphi library that provides a set of functions for working with JSON data. With this library, you can easily parse, create, manipulate, and serialize JSON data in your Delphi application. Here are some examples of how you can use the DFJsonLib library in your Delphi application: Parsing JSON data:
var
JsonStr: string;
JsonObject: TJSONObject;
begin
JsonStr := '{"name": "John", "age": 30}';
JsonObject := TDFJsonLib.Parse(JsonStr);
try
// Access JSON data
ShowMessage(JsonObject.GetValue('name').Value);
ShowMessage(IntToStr(TDFJsonLib.GetIntegerValue(JsonObject.GetValue('age'))));
finally
JsonObject.Free;
end;
end;
Creating JSON data:
var
JsonObject: TJSONObject;
JsonArray: TJSONArray;
begin
// Create JSON object
JsonObject := TDFJsonLib.CreateObject;
try
// Add properties to JSON object
TDFJsonLib.AddPair(JsonObject, 'name', TJSONString.Create('John'));
TDFJsonLib.AddPair(JsonObject, 'age', TJSONNumber.Create(30));
// Create JSON array
JsonArray := TDFJsonLib.CreateArray;
try
// Add elements to JSON array
TDFJsonLib.AddElement(JsonArray, TJSONString.Create('apple'));
TDFJsonLib.AddElement(JsonArray, TJSONString.Create('banana'));
TDFJsonLib.AddElement(JsonArray, TJSONString.Create('orange'));
// Add JSON array to JSON object
TDFJsonLib.AddPair(JsonObject, 'fruits', JsonArray);
finally
JsonArray.Free;
end;
// Serialize JSON object to string
ShowMessage(TDFJsonLib.Serialize(JsonObject));
finally
JsonObject.Free;
end;
end;
Manipulating JSON data:
var
JsonObject: TJSONObject;
begin
// Parse JSON data
JsonObject := TDFJsonLib.Parse('{"name": "John", "age": 30}');
try
// Get value of 'name' property
ShowMessage(TDFJsonLib.GetStringValue(TDFJsonLib.GetObjectProperty(JsonObject, 'name')));
// Set value of 'age' property
TDFJsonLib.SetObjectProperty(JsonObject, 'age', TJSONNumber.Create(40));
// Remove 'name' property
TDFJsonLib.RemoveObjectProperty(JsonObject, 'name');
// Serialize JSON object to string
ShowMessage(TDFJsonLib.Serialize(JsonObject));
finally
JsonObject.Free;
end;
end;
These are just a few examples of how you can use the DFJsonLib library in your Delphi application. The library provides many more functions for working with JSON data, so be sure to check out the documentation for more information.