JavaScript Object Notation
由字符串化的键值对构成,总的来说就是一个字符串表示的对象。
虽然一开始用于JavaScript,但是主要是用于web中的数据交换,就是一种数据交换格式,而且很好懂,很易读,也有利于数据传输。
{}括起来的键值对,用逗号,分隔,键值格式是键:值,json本身就是一个对象。""括起来的0~n个字符,支持转义字符[]括起来的多个值,以,分割{
"version": "test_json v1.0",
"description": "this demo used to practice json analysis",
"test_number": "78322",
"path_set": {
"load_path": ".\/data",
"save_path": ".\/result"
},
"camera_device": {
"name": "cam1",
"path": "\/sys\/dev\/",
"FOV": [
"30",
"50"
],
"RES": [
"640",
"480"
],
"Intrinsic": {
"fx": "50.2155",
"fy": "45.1245",
"cx": "0.55454",
"cy": "0.55488"
}
},
"T_Matrix": {
"t1": [
"1",
"2",
"3"
],
"t2": [
"4",
"5",
"6"
]
},
"other_info": {
"info_1": {
"info": "hello world",
"Matrix": [
"21",
"21",
"21",
"21"
]
},
"info_2": {
"info": "hello C++",
"Matrix": [
"22",
"22",
"22",
"22"
]
}
},
"location1": [
"23",
"23",
"2333"
],
"location2": [
"233",
"233",
"23333"
],
"test_choice": [
{
"name": "test1",
"enable": "true"
},
{
"name": "test2",
"enable": "false"
},
{
"name": "test1",
"enable": "true"
}
],
"end_desc": "thank you"
}
负责构造负责以json格式输出#include
namespace pt = boost::property_tree;
pt::tree root;
pt::ptree child1, child2,child3, child4;
pt::ptree childs;
//单独一个数值
child1.put("", 2);
//单独一个字符串
child2.put("", "asdh");
//对象
child3.put("key0", "val0");
//构造数组,主意childs必须只有key为""的,不能有其他。不然会退化为对象
//把数值加入数组
childs.push_back(make_pair("", child1));
childs.push_back(make_pair("", child2));
childs.push_back(make_pair("", child3));
childs.push_back(make_pair("", childs));
//键为list,值为childs数组
root.put("list", childs);
/*
{
"list":
[
2,
"asdh",
{"key0", "val0"},
[2, "asdh", {"key0", "val0"}]
]
}
*/
使用头文件#include 实现读取和写入json
#include
namespace pt = boost::property_tree;
string file_name = "./test.json";
pt::ptree json_root;
pt::read_json(file_name , json_root);
ifstream和string流stringstream都行string file_name = "./test.json";
ifstream fs;
fs.open(file_name);
pt::read_json(fs, json_root);
pt::write_json(filename, json_root);
ss.str(),cout << ss.str() << endl;pt::write_json(ss, json_root, true);
false=
{"version":"test_json v1.0","description":"this demo used to practice json analysis","test_number":"78322","path_set":{"load_path":".\/data","save_path":".\/result"},"camera_device":{"name":"cam1","path":"\/sys\/dev\/","FOV":["30","50"],"RES":["640","480"],"Intrinsic":{"fx":"50.2155","fy":"45.1245","cx":"0.55454","cy":"0.55488"}},"T_Matrix":{"t1":["1","2","3"],"t2":["4","5","6"]},"other_info":{"info_1":{"info":"hello world","Matrix":["21","21","21","21"]},"info_2":{"info":"hello C++","Matrix":["22","22","22","22"]}},"location1":["23","23","2333"],"location2":["233","233","23333"],"test_choice":[{"name":"test1","enable":"true"},{"name":"test2","enable":"false"},{"name":"test1","enable":"true"}],"end_desc":"thank you","a":{"info_1":{"info":"hello world","Matrix":["21","21","21","21"]},"info_2":{"info":"hello C++","Matrix":["22","22","22","22"]}},"b":{"load_path":".\/data","save_path":".\/result"}}
true=
{
"version":"test_json v1.0",
"description":"this demo used to practice json analysis",
"test_number":78322,
"path_set":{
"load_path":"./data",
"save_path":"./result"
},
"camera_device":{
"name":"cam1",
"path":"/sys/dev/",
"FOV":[30,50],
"RES":[640,480],
"Intrinsic":{
"fx":50.2155,
"fy":45.1245,
"cx":0.55454,
"cy":0.55488
}
},
"T_Matrix":{
"t1":[1,2,3],
"t2":[4,5,6]
},
"other_info":{
"info_1":{
"info":"hello world",
"Matrix":[21,21,21,21]
},
"info_2":{
"info":"hello C++",
"Matrix":[22,22,22,22]
}
},
"location1":[23,23,2333],
"location2":[233,233,23333],
"test_choice":[
{
"name":"test1",
"enable":true
},
{
"name":"test2",
"enable":false
},
{
"name":"test1",
"enable":true
}
],
"end_desc":"thank you"
}
add:add(key, value),add_child可以用来添加数组和对象root.add("list", "1234");
root.add("list", "1234");
/*
{
"list":"1234",
"list":"1234"
}
*/
put和put_child和add类似,不同的是put对于相同的key:value并不会重复添加,也就是root.put("list", "1234");
root.put("list", "1234");
/*
{
"list":"1234",
}
*/
push_back/push_front:提供一个pair,必须是键:对象/数组root.push_back(make_pair("123", child1));
如果单纯想插入一个非对象和数组的值,可以这样
childs2.put("", "sda");
root.push_back(make_pair("123", childs2));
string value0 = root.get<string>("key0");
double value1 = root.get<double >("key1");
int value2 = root.get<int>("key2");
bool value3 = root.get<bool>("key3");
pt::ptree value4 = root.get_child("key4");
for (pt::ptree::value_type &value : value4)
{
//2.获得每一个子节点的值,并将其放进vector
T tValue =value.second.get_value<T>();
vecData.push_back(tValue);
}
pt::ptree::iterator it;
for(it = ele_other.begin();it!=ele_other.end();it++)
{
cout << it->first << endl;;
//对每个子节点对象(即sub_value.second)进行解析
string sub_info_temp = it->second.get<string>("info");
vector<int> sub_v_temp;
bool sub_v_temp_flag = getArrayDataFromJson(sub_v_temp,it->second,"Matrix");
cout<<sub_info_temp<<"=[";
for(auto v:sub_v_temp)cout<<v<<" ";
cout<<"]"<<endl;
}
//解析普通数组
template <class T>
bool getArrayDataFromJson(std::vector<T> &vecData, pt::ptree & nodeJSON, string path) {
//1.循环遍历数组中所有的子节点(数组中的每一个元素)
for (pt::ptree::value_type &value : nodeJSON.get_child(path))
{
//2.获得每一个子节点的值,并将其放进vector
T tValue =value.second.get_value<T>();
vecData.push_back(tValue);
}
return true;
}
root.erase("list");
root.begin()->second.put_value("wasdasd");
root.get_child("list").put_value("asd");