C++ 操作json文件

一、环境搭建:

参考文章:https://blog.csdn.net/fakine/article/details/79272090

二、创建实例:

#include <stdio.h>
#include <direct.h>
#include <io.h>
#include <sys/stat.h>
#include <string> 
#include <json.h> 
#include <iostream> 
#include <fstream> 
using namespace std;

void MyMethod::creatJsonFile(void)
{
//根节点 
Json::Value root;
//根节点属性1
root["value1"] = "value_1";
root["root1"]["value_1_1"] = Json::Value("value_1_1");

root["array"].append("member1");
root["array"].append("member2");
root["array"].append("member3");


cout << "StyledWriter:" << endl;
Json::StyledWriter sw;
cout << sw.write(root) << endl;
}

调用:MyMethod::creatJsonFile();
生成结果如下:
StyledWriter:
{
"array" : [ "member1", "member2", "member3" ],
"root1" : {
"value_1_1" : "value_1_1"
},
"value1" : "value_1"
}
坚持成就伟大
原文地址:https://www.cnblogs.com/xian-yongchao/p/10241732.html