JSON is a text-based data format following JavaScript object syntax, which was popularized by Douglas Crockford. Even though it closely resembles JavaScript object literal syntax, it can be used independently from JavaScript, and many programming environments feature the ability to read (parse) and generate JSON. JSON exists as a string, and is useful when you want to transmit data across a network no matter System, Architecture or Programming Language.
One of the more powerful Libraries for handling JSON strings with Delphi is Winsoft JSON Library. Is very simple to use but very versatile to work with.
Installation
Download the latest version here, and unzip the downloaded file to your default components folder.
This is a non-visual library, so you don’t need to install it on IDE.
Add a folder according to your Delphi version and Platform in Tools->Options->Language->Delphi->Library to Library Path
Sample
Reading and parsing JSON Files
In the following example we are going to implement a generic JSON viewer that shows structure of the selected JSON file.
You can open this sample project at <Winsoft JSON Install Folder>\Examples\Delphi\VCL\Demo.dpr
The following code takes some steps to start parsing of a JSON file
- Load a JSON file you select from Open dialog
- Create instance of TJson object
- Load content from selected file to Tjson object via
ParseUtg8File(OpenDialog.FileName)
- Call recursive function
Show(nil, '' jsonItem)
for root node
This function starts checking node data type via some predefined item classes like TJsonNull, TJsonTrue, TJsonFalse, TJsonString
and adding to Treeview. If the item being checked is an array, the TJsonArray
function is called recursively with new parameters in order to add array elements as child of current item. Checking for TJsonObject
add object properties to with current object as parent
Like what you see? You can get JSON and over 100 other fantastic WinSoft components with our Enterprise Component Pack. For a limited time, when you purchase RAD Studio Enterprise or Architect Edition at special Upgrade Pricing, you will also get this package of third-party software worth over $13,000, including the full WinSoft Component Library, at NO EXTRA COST! Step up to RAD Studio 10.4 today!