![jsonfeaturedimage | Embarcadero RAD Studio Delphi C++Builder Blogs](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2020/08/json-featured-image.png?resize=1140%2C675&ssl=1)
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.
![image4 | Embarcadero RAD Studio Delphi C++Builder Blogs](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2020/08/image-4.png?resize=406%2C305&ssl=1)
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
![image5 | Embarcadero RAD Studio Delphi C++Builder Blogs](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2020/08/image-5.png?resize=262%2C381&ssl=1)
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
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 |
procedure TFormMain.ButtonOpenFileClick(Sender: TObject); var JsonItem: TJson; begin if OpenDialog.Execute then begin TreeView.Items.BeginUpdate; try TreeView.Items.Clear; with TJsonParser.Create do try JsonItem := ParseUtf8File(OpenDialog.FileName); try Show(nil, '', JsonItem); TreeView.FullExpand; finally JsonItem.Free; end; finally Free; end; finally TreeView.Items.EndUpdate; end; end; end; |
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
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 |
procedure TFormMain.Show(Parent: TTreeNode; const Prefix: string; JsonItem: TJson); var Child: TTreeNode; JsonArray: TJsonArray; JsonObject: TJsonObject; i: Integer; begin with TreeView.Items do if JsonItem is TJsonNull then AddChild(Parent, Prefix + 'null') else if JsonItem is TJsonFalse then AddChild(Parent, Prefix + 'false') else if JsonItem is TJsonTrue then AddChild(Parent, Prefix + 'true') else if JsonItem is TJsonNumber then AddChild(Parent, Prefix + FloatToStr(TJsonNumber(JsonItem).Value)) else if JsonItem is TJsonString then AddChild(Parent, Prefix + '"' + TJsonString(JsonItem).Value + '"') else if JsonItem is TJsonArray then begin Child := AddChild(Parent, Prefix + '[]'); JsonArray := TJsonArray(JsonItem); for i := 0 to JsonArray.ElementCount - 1 do Show(Child, '[' + IntToStr(i) + '] ', JsonArray.Elements[i]); end else if JsonItem is TJsonObject then begin Child := AddChild(Parent, Prefix + '{}'); JsonObject := TJsonObject(JsonItem); for i := 0 to JsonObject.MemberCount - 1 do Show(Child, JsonObject.MemberName[i] + ': ', JsonObject.MemberValue[i]); end; end; |
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!
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition