Reader small image

You're reading from  Qt 6 C++ GUI Programming Cookbook - Third Edition

Product typeBook
Published inApr 2024
PublisherPackt
ISBN-139781805122630
Edition3rd Edition
Right arrow
Author (1)
Lee Zhi Eng
Lee Zhi Eng
author image
Lee Zhi Eng

Lee Zhi Eng is a self-taught programmer who worked as an artist and programmer at several game studios before becoming a part-time lecturer for 2 years at a university, teaching game development subjects related to Unity and Unreal Engine. He has not only taken part in various projects related to games, interactive apps, and virtual reality but has also participated in multiple projects that are more oriented toward software and system development. When he is not writing code, he enjoys traveling, photography, and exploring new technologies.
Read more about Lee Zhi Eng

Right arrow

JSON Parsing Made Easy

JSON is the file extension of a data format called JavaScript Object Notation, which is used to store and transport information in a structured format. The JSON format is used extensively for the web. Most modern web Application Programming Interfaces (APIs) use JSON format to transfer data to their web clients.

This chapter will cover the following recipes:

  • JSON format in a nutshell
  • Processing JSON data from a text file
  • Writing JSON data to a text file
  • Using Google’s Geocoding API

Technical requirements

The technical requirements for this chapter include Qt 6.6.1 MinGW 64-bit and Qt Creator 12.0.2. All the code used in this chapter can be downloaded from the following GitHub repository: https://github.com/PacktPublishing/QT6-C-GUI-Programming-Cookbook---Third-Edition-/tree/main/Chapter10.

JSON format in a nutshell

JSON is a human-readable text format commonly used for data transmission in web applications, especially JavaScript applications. However, it is also used for many other purposes, hence it is independent of JavaScript and can be used for any programming language or platform, despite its name.

In this example, we will learn about JSON format and how to verify whether your JSON data is in a valid format or not.

How to do it…

Let’s get started and learn how to write your own JSON data and verify its format:

  1. Open up your web browser and go to the JSONLint Online Validator and Formatter website at https://jsonlint.com.
  2. Write the following JSON data in the text editor on the website:
    {
        "members": [
            {
                "name": "John",
           ...

Processing JSON data from a text file

In this recipe, we will learn how to process JSON data taken from a text file and extract it using the stream reader.

How to do it…

Let’s create a simple program that reads and processes XML files by following these steps:

  1. Create a new Qt Widgets Application project at your desired location.
  2. Open any text editor and create a JSON file that looks like the following, then save it as scene.json:
    [
        {
            "name": "Library",
            "tag": "building",
            "position": [120.0, 0.0, 50.68],
            "rotation": [0.0, 0.0, 0.0],
            "scale": [1.0, 1.0, 1.0]
        }
    ]
  3. Continue to write the JSON code by adding...

Writing JSON data to a text file

Since we have learned how to process data obtained from a JSON file in the previous recipe, we will move on to learning how to save data to a JSON file. We will continue with the previous example and add to it.

How to do it…

We will learn how to save data in a JSON file through the following steps:

  1. Add another button to mainwindow.ui, then set its object name as saveJsonButton and its label as Save JSON:
Figure 10.4 – Adding the Save JSON button

Figure 10.4 – Adding the Save JSON button

  1. Right-click on the button and select Go to slot…. A window will pop up with a list of signals available for selection. Select the clicked() option and click OK. A signal function called on_saveJsonButton_clicked() will now be automatically added to both your mainwindow.h and mainwindow.cpp files by Qt:
Figure 10.5 – Selecting the clicked() signal and pressing OK

Figure 10.5 – Selecting the clicked() signal and pressing OK

  1. Add the following code to the...

Using Google’s Geocoding API

In this example, we will learn how to obtain the full address of a specific location by using Google’s Geocoding API.

How to do it…

Let’s create a program that utilizes the Geocoding API by following these steps:

  1. Create a new Qt Widgets Application project.
  2. Open mainwindow.ui and add a couple of text labels, input fields, and a button to make your UI look similar to this:
Figure 10.7 – Setting up your UI

Figure 10.7 – Setting up your UI

  1. Open your project (.pro) file and add the network module to your project. You can do that by simply adding the word network after core and gui, as shown in the following code:
    QT += core gui network
  2. Open mainwindow.h and add the following headers to the source code:
    #include <QMainWindow>
    #include <QDebug>
    #include <QtNetwork/QNetworkAccessManager>
    #include <QtNetwork/QNetworkReply>
    #include <QJsonDocument>
    #include <QJsonArray>...
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Qt 6 C++ GUI Programming Cookbook - Third Edition
Published in: Apr 2024Publisher: PacktISBN-13: 9781805122630
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Lee Zhi Eng

Lee Zhi Eng is a self-taught programmer who worked as an artist and programmer at several game studios before becoming a part-time lecturer for 2 years at a university, teaching game development subjects related to Unity and Unreal Engine. He has not only taken part in various projects related to games, interactive apps, and virtual reality but has also participated in multiple projects that are more oriented toward software and system development. When he is not writing code, he enjoys traveling, photography, and exploring new technologies.
Read more about Lee Zhi Eng