系列文章目录
文章目录
前言
一、JSON
1、jsoncpp
jsoncpp 是cpp对json数据格式的调用库,需要安装jsoncpp,cpp才能使用json数据格式。
jsoncpp的安装参考以下链接
https://blog.csdn.net/wesigj/article/details/116643589
2、nlohmann/json
nlohmann/json 是一个用于解析json的开源c++库,是一个header only的库,也就是说只要包含一个.hpp文件,那么你就可以使用该库。
源码:
https://github.com/nlohmann/json/blob/develop/single_include/nlohmann/json.hpp
二、使用步骤
1、json文件
代码如下:
test.json
{
"filename" : "example.json",
"string" : "Hello World!",
"filesize" : 1024,
"inttype" : 123,
"floattype" : 3.14,
"doubletype": 1.2345,
"arrayint" : [1001,1002,1003,1004],
"arraycahr": ["a", "b", "c", "d"],
"arrayobj1" : [{"a":1, "b":2},{"c":3},{"d":4}],
"arrayobj2" : [{"name":"张三","age":18,"weight":60.3},{"name":"李四","age":19,"weight":66.6}]
}
2、jsoncpp从json文件中读取json
#include <jsoncpp/json/json.h>
#include <iostream>
#include <string>
#include <fstream>
#include <cassert>
using namespace std;
void demo_simple()
{
ifstream ifs;
ifs.open("test.json");
//.cpp文件当前目录可以直接打开,否则相对路径和绝对路径也可以
assert(ifs.is_open());
Json::Reader reader;
Json::Value root;
if (!reader.parse(ifs, root, false))
{
cerr << "parse failed \n";
return;
}
// string name = root["pathType"].asString();
// int age = root["inttype"].asInt();
// cout << "demo read from filet result =============\n";
// cout << "pathType : " << name << endl;
// cout << "inttype: " << age << endl;
Json::Value array = root["arraycahr"];
for(int i = 0;i < array.size();i++)
{
std::cout<<array[i]<<endl;
// std::cout<<array[i].asString()<<endl;
}
// cout<<array[0]["a"]<<endl;
ifs.close();
}
int main()
{
demo_simple();
return 0;
}
#include <jsoncpp/json/json.h>
// #include <cstdio>
// #include <cstring>
// #include <string>
// #include <iostream>
//json
#include <iostream>
#include <string>
#include <fstream>
#include <cassert>
using namespace std;
void demo_simple()
{
ifstream ifs;
ifs.open("../scripts/json_structure/mediator2ros.json");
assert(ifs.is_open());
Json::Reader reader;
Json::Value root;
if (!reader.parse(ifs, root, false))
{
cerr << "parse failed \n";
return;
}
Json::Value name = root["arraycahr"];
Json::Value age = root["inttype"];
// string name = root["filename"].asString();
// int age = root["inttype"].asInt();
cout << "demo read from filet result =============\n";
cout << "pathType : " << name << endl;
cout << "inttype : " << age << endl;
// Json::Value array = root["arraycahr"];
// for(int i = 0;i < array.size();i++)
// {
// std::cout<<array[i]<<endl;
// // std::cout<<array[i].asString()<<endl;
// }
// cout<<array[0]["a"]<<endl;
ifs.close();
}
int main()
{
// string test ="{\"id\":1,\"name\":\"kurama\"}";
// Json::Reader reader;
// Json::Value value;
// if(reader.parse(test,value))
// {
// if(!value["id"].isNull())
// {
// cout<<value["id"].asInt()<<endl;
// cout<<value["name"].asString()<<endl;
// }
// }
demo_simple();
return 0;
}
3、nlohmann/json 从json文件中读取json
#include "json.hpp"
//json
#include <iostream>
#include <string>
#include <fstream>
#include <cassert>
// using namespace std;
// for convenience
using json = nlohmann::json;
void demo_simple()
{
json j;
std::ifstream("../scripts/mediator_json/mediator2ros.json") >> j;
// std::cout<<j<<std::endl;
// std::cout<< j["doubletype"]<<std::endl;
// std::ifstream read("../scripts/json_structure/mediator2ros.json");
// json in = json::parse(read);
// json in = json::parse(std::ifstream("../scripts/mediator_json/mediator2ros.json"));
// std::cout << in.dump(4) << std::endl;
json _robot_state = json::parse(std::ifstream("../scripts/state_api_json/robot_state.json"));
std::cout << _robot_state.dump(4) << std::endl;
//一行代码从json文件读取json
//json _relocation_manual = json::parse(std::ifstream("../scripts/ros/control_api_json/relocation_manual.json"));
}
int main()
{
demo_simple();
return 0;
}
总结
以上就是今天要讲的内容
本文作者:WeSiGJ
参考链接(包括但不限于):
https://github.com/nlohmann/json
https://www.cnblogs.com/maizhongfei/p/14164895.html
欢迎各位兄弟姐妹们,加入C++ HTTP服务器开发技术交流群:
QQ群:426685924
![](https://img-blog.csdnimg.cn/20210520100231608.png)