溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C++?qt如何使用jsoncpp?json進(jìn)行讀寫操作

發(fā)布時(shí)間:2021-11-26 13:14:48 來(lái)源:億速云 閱讀:360 作者:柒染 欄目:開(kāi)發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)C++ qt如何使用jsoncpp json進(jìn)行讀寫操作,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

JsonCpp的使用

項(xiàng)目需要c++下使用json,我選擇了JsonCpp,官網(wǎng)是:https://github.com/open-source-parsers/jsoncpp。
解壓后使用python編譯出兩個(gè)h文件和一個(gè)cpp文件:

(電腦需要安裝python自己百度安裝,這里就不說(shuō)了)

安裝python后,打開(kāi)windows下cmd窗口,進(jìn)入到j(luò)soncpp文件夾  如圖:

C++?qt如何使用jsoncpp?json進(jìn)行讀寫操作

執(zhí)行命令:python amalgamate.py 就會(huì)生成dist文件夾 里面有 json.h json-forwards.h jsoncpp.cpp三個(gè)文件:如下

C++?qt如何使用jsoncpp?json進(jìn)行讀寫操作

將三個(gè)文件加入到工程即可使用,我是要qt進(jìn)行測(cè)試使用:

C++?qt如何使用jsoncpp?json進(jìn)行讀寫操作

main.cpp如下

#include <iostream>
#include <fstream>
#include "dist/json/json.h"
using namespace std;
 
int main(int argc, char *argv[])
{
    // write
    Json::Value people1;
    people1["name"] = "Dione";
    people1["sex"] = "男";
    people1["age"] = 24;
    people1["note"] = "jsoncpp write test!";
 
    Json::Value people2;
    people2["name"] = "Hulis";
    people2["sex"] = "女";
    people2["age"] = 22;
    people2["note"] = "jsoncpp write test!";
 
    Json::Value peoples;
    peoples.append(people1);
    peoples.append(people2);
 
    Json::Value writeValue;
    writeValue["classname"] = "三年一班";
    writeValue["peoples"] = peoples;
 
 
    Json::FastWriter fwriter;
    std::string strf = fwriter.write(writeValue);
    std::ofstream ofsf("example_fast_writer.json");
    ofsf << strf;
    ofsf.close();
 
    Json::StyledWriter swriter;
    std::string strs = swriter.write(writeValue);
    std::ofstream ofss("example_styled_writer.json");
    ofss << strs;
    ofss.close();
 
    // read
    string strValue = "{\"key1\":\"111\",\"array\":[{\"key2\":\"222\"},{\"key2\":\"333\"},{\"key2\":\"444\"}]}";
    Json::Reader reader;
    Json::Value root;
    if (reader.parse(strValue, root))
    {
        std::string out = root["key1"].asString();
        qDebug()<<QString::fromStdString(out);
        Json::Value arrayObj = root["array"];
        for (int i=0; i<arrayObj.size(); i++)
        {
            out = arrayObj[i]["key2"].asString();
            qDebug()<<QString::fromStdString(out);
        }
    }
 
    std::ifstream ifs("example_fast_writer.json");
    if (reader.parse(ifs, root))
    {
        std::string out = root["classname"].asString();
        qDebug()<<QString::fromStdString(out);
        Json::Value peoples = root["peoples"];
        for (int i=0; i<peoples.size(); i++)
        {
            qDebug()<<QString::fromStdString(peoples[i]["name"].asString());
            qDebug()<<QString::fromStdString(peoples[i]["sex"].asString());
            qDebug()<<QString::fromStdString(peoples[i]["age"].asString());
            qDebug()<<QString::fromStdString(peoples[i]["note"].asString());
        }
    }
 
    return 0;
}

會(huì)生成兩個(gè)json文件,一個(gè)是沒(méi)有格式寫入一個(gè)是有格式寫入,如下:

C++?qt如何使用jsoncpp?json進(jìn)行讀寫操作

關(guān)于C++ qt如何使用jsoncpp json進(jìn)行讀寫操作就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI