1.配置jsoncpp库的环境:
jsoncpp,百度云盘下载:
链接:https://pan.baidu.com/s/1ep4wQdt3diHvbqXtIQBxKw
提取码:0hlg
下载完成并解压:
源码方式部署:
Visual Studio 2015:
方式1:直接链接到jsoncpp的安装包
1.1 VC++包含目录添加jsoncpp的安装包中的include目录
解决方案资源管理器:右击项目->属性->VC++目录->包含目录:
添加jsoncpp的安装包中的include目录
1.2.加载jsoncpp库的cpp文件
解决方案资源管理器:右击源文件->添加->添加现有文件:
添加jsoncpp安装包/src/lib_json
目录下的cpp文件:
json_reader.cpp,json_value.cpp,json_writer.cpp
然后环境部署完毕。
方式2:将jsoncpp中必要文件布置在项目目录内(方便移植,不需再依赖)
1.将jsoncpp的安装包中的include目录拷贝到项目目录下:
2.VC++包含目录添加include相对路径
注:VS中,.\
表示当前路径,..\
表示上一级路径;
当前位置就是.vcproj
文件所在的位置;
解决方案资源管理器:右击项目->属性->VC++目录->包含目录:
.\include
3.拷贝cpp文件并添加
jsoncpp安装包/src/lib_json
目录下的cpp文件拷贝到项目目录下并添加到项目中:
json_reader.cpp,json_value.cpp,json_writer.cpp
然后即可运行。
QT的布置:
和VS类似:
拷贝include文件夹和3个cpp文件到项目目录下,
然后在pro文件中添加include的路径即可
$${PWD}/include
2.jsoncpp的读取:
json文件内容:
{
"channel_id":3,
"image":{
"method":"Area",
"image_size":[720,1280],
"th":0.85,
"cls":["animal","car","person"],
"lineArea": [
{"x1": 59, "y1": 210, "x2": 90, "y2": 311, "kmin": 10, "kmax": 1000},
{"x1": 190, "y1": 147, "x2": 233, "y2": 175, "kmin": 0, "kmax": 1.5},
{"x1": 554, "y1": 198, "x2": 586, "y2": 249, "kmin": 0, "kmax": 1.5},
{"x1": 244, "y1": 399, "x2": 395, "y2": 425, "kmin": 0, "kmax": 1.5}
]
}
}
代码如下:
#include <json/json.h>
#include <fstream>
#include <iostream>
using namespace std;
struct linearea
{
int x1,x2,y1,y2;
double kmin,kmax;
};
struct config_params
{
string method;
int channel_id;
double th;
int img_height;
int img_width;
vector<string> cls;
vector<lineArea> vec_linearea
};
int main()
{
Json::Reader reader;
Json::Value root;
//1.从文件中读取,保证当前文件有demo.json文件
ifstream in("E:\\workspace\\py_project\\predict\\rect.json", ios::binary);
if (!in.is_open())
{
cout << "Error opening file\n";
return 0;
}
//2. 用root去读取json文件内容
if (!reader.parse(in, root))
{
cout << "读取json内容出现错误,极可能是json中有格式错误或混有中 文字符" << endl;
return 0;
}
//从root中提取我们需要的json数据
//我们模拟一个结构体,对应接收接送文件中的数据
config_params config;
// 3.1 读取int数据
config.channel_id = root["channel_id"].asInt();
//3.2读取string数据,注意读取结构体内数据的格式:
config.method= root["image"]["method"].asString();
//3.3读取浮点型数据
config.th = root["image"]["th"].asDouble();
//3.4读取一个数组,我们可以才有多个变量来接收一个数组
//注意!!!如果要直接写出root中数组的下标,需要在数字后加上字母u,否则报错
config.img_height=root["image"]["image_size"][0u].asInt();
config.img_width=root["image"]["image_size"][1u].asInt();
//3.5读取一个复杂数组,结构体作为元素,我们可以自己定义一个结构体来对接数据
//这一次采用循环读取数组,数组下标会用变量替代,后面不需要加u
for(int i=0; i<4;i++)
{
linearea.x1 = root["image"]["lineArea"][i]["x1"].asInt();
linearea.x2 = root["image"]["lineArea"][i]["x2"].asInt();
linearea.y1 = root["image"]["lineArea"][i]["y1"].asInt();
linearea.y2 = root["image"]["lineArea"][i]["y2"].asInt();
linearea.kmin = root["image"]["lineArea"][i]["kmin"].asDouble();
linearea.kmax = root["image"]["lineArea"][i]["kmax"].asDouble();
config.vec_linearea.push_back(linearea);
}
return 1;
}
面对更复杂的json,无非就是基本方法的延伸,如何处理结构体数据,数组数据,结构体数组可以参考代码。
3.jsoncpp写json:
读写差异不大,很好理解,直接代码演示:
代码:
#include <iostream>
#include <fstream>
#include <json/json.h>
using namespace std;
int main()
{
Json::Value root; //根节点
//编写root
root["project"] = "classify";
root["base_params"]["height"] = 480;
root["base_params"]["width"] = 640;
root["base_params"]["channels_num"] = 1;
// 注意,数字后面需要加上字母u
root["params"][0u]["name"] = "car";
root["params"][0u]["flag"] = 0;
root["params"][0u]["th"] = 0.45;
root["params"][1u]["name"] = "person";
root["params"][1u]["flag"] = 1;
root["params"][1u]["th"] = 0.85;
// 创建json
fstream f;
f.open ("test.json", ios::out);
if( !f.is_open() ){
cout<< "Open file error!" <<endl;
}
f << root.toStyledString(); //转换为json格式并存到文件流
f.close();
}
输出结果:
{
"base_params" : {
"channels_num" : 1,
"height" : 480,
"width" : 640
},
"params" : [
{
"flag" : 0,
"name" : "car",
"th" : 0.450
},
{
"flag" : 1,
"name" : "person",
"th" : 0.850
}
],
"project" : "classify"
}