我使用的环境之 ubuntu14.04 + ros-indigo
1.安装jsoncpp库 终端运行
$ sudo apt-get install libjsoncpp-dev libjsoncpp0
2.1 好用jsoncpp 读取example.json文件内用
example.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" : ["a1":1,"a2":"a2":2,"a3":3]
"arrayobj2" : [{"name":"张三","age":18,"weight":60.3},{"name":"李四","age":19,"weight":66.6}]
}
2.2读取实例代码
#include
#include
#include
#include
using namespace std;
int main()
{
Json::Value root;
Json::Reader reader;
std::ifstream ifs("example.json");//open file example.json
if(ifs==NULL)
{
}
if(!reader.parse(ifs, root)){
// fail to parse
}
else{
// success
std::cout<
2.3编译需要指定库
$ g++ jsoncpp.cpp -l jsoncpp -o jsoncpp.out
3.1 jsoncpp构建json数据保存到json 文件
#include
#include
#include
#include
using namespace std;
int main()
{
Json::Value root;
Json::Reader reader;
Json::FastWriter fwriter;
Json::StyledWriter swriter;
Json::Value array;
std::ifstream ifs("waypoint.json");//open file example.json
if(ifs==NULL)
{
}
if(!reader.parse(ifs, root)){
// fail to parse
}
else{
// success
array = root["array"];
std::cout<
3.2编译需要指定库
$ g++ jsoncpp.cpp -l jsoncpp -o jsoncpp.out
附文件:
waypoint.json输入文件
{
"type" : "point",
"array" : [
{"x":1,"y":21,"z":31,"w":0.1},
{"x":2,"y":22,"z":32,"w":0.2},
{"x":3,"y":23,"z":33,"w":0.3},
{"x":4,"y":24,"z":34,"w":0.4}
]
}
example_fast_writer.json输出文件
[{"w":0.10,"x":1234,"y":21,"z":31},{"w":0.20,"x":1234,"y":22,"z":32},{"w":0.30,"x":1234,"y":23,"z":33},{"w":0.40,"x":1234,"y":24,"z":34}]
example_styled_writer.json输出文件
{
"array" : [
{
"w" : 0.10,
"x" : 1234,
"y" : 21,
"z" : 31
},
{
"w" : 0.20,
"x" : 1234,
"y" : 22,
"z" : 32
},
{
"w" : 0.30,
"x" : 1234,
"y" : 23,
"z" : 33
},
{
"w" : 0.40,
"x" : 1234,
"y" : 24,
"z" : 34
}
],
"type" : "point"
}
ROS下使用jsoncpp库此外
3.1 在 头文件 加入
#include
3.1在CMakeList.txt文件 target_link_libraries(jsoncpp ${catkin_LIBRARIES}) 加入jsoncpp