JSON (JavaScript Object Notation) is a language-independent lightweight data-interchange format. JSON can be used as an alternative to other data-interchange formats such as XML or YAML. RAD Studio provides JSON frameworks that contain classes and methods to store, parse, read, write, and generate data in JSON format. Sooner or later you will find it necessary to understand what JSON is and how to use it since it’s an extremely common format, especially for things like REST servers.
In this post, you’ll learn what JSON is, How we can test a JSON script? How we can use JSON with C++ Builder? Can we generate and parse JSON in C++ Builder? By learning more about JSON, it will help you build C++ applications with the use of C++ software.
Table of Contents
JSON Frameworks
RAD Studio provides different frameworks to handle JSON data.
- JSON Objects Framework: This framework creates temporary objects to read and write JSON data.
- Readers and Writers JSON Framework: This framework allows you to read and write JSON data directly.
JSON Example
For example you have animals in a Zoo and you want to hold data in JSON form. In our small zoo, let’s have two Penguins from different origins
- First one is from Magascar at the age of 4
- Second one is from Antarctica at the age of 5
In JSON form this data can be written in animals category with their properties as below,
You can test this JSON with a JSON Parser Online , here below is from http://json.parser.online.fr/
JSON Generator Example in C++ Builder
In C++ Builder, we can create this JSON form by using TJSONObject and TJSONArray. We can add properties of animals by the AddPair() method and we can use AddElement() method to add these animals with their features. Let’s do this in a new C++ Builder Project.
- Create a new C++ Builder Multi-Device application, save all project and unit files to a folder.
- Add a TMemo and two TButtons with text “Generate” and “Parse”
- To use JSON methods, we need to include System.JSON .hpp, with #include <System.JSON.hpp> as below,
4. To create this JSON form, first we should create a jobject with TJSONObject and jarray with TJSONArray then we can create elements with TJSONObject to define each animal properties. We can add these elements to our array by using AddElement() method. Here is a simplified code below,
After using JSON object we should free this, all it’s elements will be free too. To make safe data addition, we can use __try and__finally. Here is a simple example to show how we can add all elements safely as below.
If we run this example with a C++ Builder VCL or FMX application, we will have same JSON result.
JSON Parser Example in C++ Builder
We can parse each objects and elements of arrays as given example below. Let’s get this JSON output above from the Memo component and parse.
Here is the example output,
You can check Official DockWiki http://docwiki.embarcadero.com/ for more details and latest news about JSON format. Also don’t forget to check JSON.org for more details about JSON format. If you still have questions JSON in C++ Builder and Delphi is well explained here ChapmanWorld with examples.