簡體   English   中英

"如何讀取和使用 JSON 文件中的數據"

[英]How to read and use data ina JSON file

我正在尋找一個 json 文件並使用該信息來創建一個多項選擇測驗。 我只是無法理解如何從 JSON 文件中實際讀取它。 我已經成功地閱讀了正確的問題數量,但僅此而已。

這是目前我的 JSON 文件的布局:

{
    "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
        }
    ]
}

Nlohmann JSON可能是最容易用於您的應用程序的,它遵循現代 C++ 原則。 單個包含(僅標頭實現)和 JSON 文件放在項目目錄中。

示例用法:

#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;
}

輸出:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM