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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM