温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C++ qt如何使用jsoncpp json进行读写操作

发布时间:2021-11-26 13:14:48 来源:亿速云 阅读:345 作者:柒染 栏目:开发技术

这篇文章将为大家详细讲解有关C++ qt如何使用jsoncpp json进行读写操作,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

JsonCpp的使用

项目需要c++下使用json,我选择了JsonCpp,官网是:https://github.com/open-source-parsers/jsoncpp
解压后使用python编译出两个h文件和一个cpp文件:

(电脑需要安装python自己百度安装,这里就不说了)

安装python后,打开windows下cmd窗口,进入到jsoncpp文件夹  如图:

C++ qt如何使用jsoncpp json进行读写操作

执行命令:python amalgamate.py 就会生成dist文件夹 里面有 json.h json-forwards.h jsoncpp.cpp三个文件:如下

C++ qt如何使用jsoncpp json进行读写操作

将三个文件加入到工程即可使用,我是要qt进行测试使用:

C++ qt如何使用jsoncpp json进行读写操作

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

会生成两个json文件,一个是没有格式写入一个是有格式写入,如下:

C++ qt如何使用jsoncpp json进行读写操作

关于C++ qt如何使用jsoncpp json进行读写操作就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI