Cavan Phelan
Cavan Phelan

Reputation: 41

How to read and use data in a JSON file using C++

I'm looking to read a JSON file and use that information to create a multiple choice quiz. I'm just having trouble understanding how to actually read it in from a JSON file. I've managed to read in the number of questions right but that is all.

This is currently the layout of my JSON file:

{
    "numOfQues": 5,
    "questions": [
        {
            "question": "Who is the US President?",
            "options": [
              "Joe Biden",
              "Joe BIREN",
              "Joe Momma",
              "Joe Bein"
            ],
            "answer": 2
        },
        {
            "question": "Who scored the best goal in Puskas history?",
            "options": [
              "Erik Lamela",
              "Son Heung-Min",
              "Cristiano Ronaldo",
              "Wayne Rooney"
            ],
            "answer": 4
        },
        {
            "question": "Where should Lamela really have finished?",
            "options": [
              "First",
              "Second",
              "Third",
              "Fourth"
            ],
            "answer": 3
        },
        {
            "question": "What does Conor love?",
            "options": [
              "Breaking curbs",
              "Breathing",
              "Having shit football opinions",
              "Being from Carlow"
            ],
            "answer": 1
        },
        {
            "question": "Who is the best footballer ever?",
            "options": [
              "Eric Dier",
              "Emile Heskey",
              "Bobby Zamora",
              "Phil Jones"
            ],
            "answer": 4
        }
    ]
}

Upvotes: 4

Views: 15409

Answers (1)

dterification
dterification

Reputation: 123

Nlohmann JSON is likely the easiest to use for your application and it follows modern C++ principles. Place the single include (header-only implementation) and JSON file in your project directory.

Example usage:

#include "json.hpp"
#include <fstream>
#include <iostream>

using namespace std;

int main()
{
    ifstream fJson("questions.json");
    stringstream buffer;
    buffer << fJson.rdbuf();
    auto json = nlohmann::json::parse(buffer.str());

    cout << "\nNumber of questions: " << json["numOfQues"] << "\n";

    for (auto question : json["questions"])
    {

        cout << question["question"] << "\n\n";
        int qCount = 0;
        for (auto opt : question["options"])
        {
            qCount++;
            cout << qCount << ". " << opt << "\n";
        }
        cout << "Answer: " << question["answer"] << "\n";
    }

    return 0;
}

Output:

Number of questions: 5
"Who is the US President?"

1. "Joe Biden"
2. "Joe BIREN"
3. "Joe Momma"
4. "Joe Bein"
Answer: 2

"Who scored the best goal in Puskas history?"

1. "Erik Lamela"
2. "Son Heung-Min"
3. "Cristiano Ronaldo"
4. "Wayne Rooney"
Answer: 4

"Where should Lamela really have finished?"

1. "First"
2. "Second"
3. "Third"
4. "Fourth"
Answer: 3

"What does Conor love?"

1. "Breaking curbs"
2. "Breathing"
3. "Having shit football opinions"
4. "Being from Carlow"
Answer: 1

"Who is the best footballer ever?"

1. "Eric Dier"
2. "Emile Heskey"
3. "Bobby Zamora"
4. "Phil Jones"
Answer: 4

Upvotes: 12

Related Questions