C++ qt 使用jsoncpp json 读写

JsonCpp的使用

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

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

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

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

 

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

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文件,一个是没有格式写入一个是有格式写入,如下:

Demo工程下载地址:https://download.csdn.net/download/u012532263/10674215

仅供学习参考使用,谢谢 ! by Dione

 

 

 

 

 

  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在C++使用JsonCpp库来读取JSON文件,你可以按照以下步骤进行操作: 1. 首先,确保已经下载并安装了JsonCpp库,并将其包含在你的项目中。 2. 创建一个Json::Value对象来存储解析后的JSON数据。 3. 使用Json::Reader对象来读取JSON文件并解析为Json::Value对象。 下面是一个简单的示例代码,演示了如何使用JsonCpp读取JSON文件: ```cpp #include <iostream> #include <fstream> #include <json/json.h> int main() { // 读取 JSON 文件 std::ifstream jsonFile("example.json"); if (!jsonFile.is_open()) { std::cout << "Failed to open JSON file." << std::endl; return 1; } // 解析 JSON 数据 Json::Value root; Json::Reader reader; if (!reader.parse(jsonFile, root)) { std::cout << "Failed to parse JSON data." << std::endl; return 1; } // 读取 JSON 数据并输出 std::string name = root["name"].asString(); int age = root["age"].asInt(); std::string city = root["city"].asString(); std::cout << "Name: " << name << std::endl; std::cout << "Age: " << age << std::endl; std::cout << "City: " << city << std::endl; return 0; } ``` 在上面的示例中,我们首先打开了一个名为`example.json`的JSON文件,然后使用JsonCppJson::Reader对象解析JSON数据,并将其存储在名为`root`的Json::Value对象中。接下来,我们通过访问`root`对象的键值对来获取JSON数据,并将其输出到控制台上。 请确保将示例代码中的`example.json`替换为你实际使用JSON文件的路径。 这里提供一个CSDN上的相关教程供参考:[https://blog.csdn.net/qq_41453285/article/details/105808014](https://blog.csdn.net/qq_41453285/article/details/105808014)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值