Site icon Learn C++

You NEED to Learn To Use JSON (JavaScript Object Notation)

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.

JSON Frameworks

RAD Studio provides different frameworks to handle JSON data.

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

In JSON form this data can be written in animals category with their properties as below,

[crayon-6741adca8c115205767478/]

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.

  1. Create a new C++ Builder Multi-Device application, save all project and unit files to a folder.
  2. Add a TMemo and two TButtons with text “Generate” and “Parse”
  3. To use JSON methods, we need to include System.JSON .hpp, with #include <System.JSON.hpp> as below,
[crayon-6741adca8c11d939169048/]

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,

[crayon-6741adca8c11f048759918/]

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.

[crayon-6741adca8c120791143245/]

If we run this example with a C++ Builder VCL or FMX application, we will have same JSON result.

[crayon-6741adca8c121739336814/]

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.

[crayon-6741adca8c122525412135/]

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.

Exit mobile version