ROS 使用C++ jsoncpp库 读写保存json.json 文件

我使用的环境之 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<string>
#include<iostream>
#include<fstream>
#include <jsoncpp/json/json.h> 
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<<root["filename"].asString()<<endl;
   std::cout<<root["string"].asString()<<endl;
   std::cout<<root["filesize"].asInt()<<endl;
   std::cout<<root["inttype"].asInt()<<endl;
   std::cout<<root["floattype"].asFloat()<<endl;
   std::cout<<root["doubletype"].asDouble()<<endl;
   Json::Value array = root["arrayint"];
   for(int i = 0;i < array.size();i++)
   {
	   std::cout<<array[i].asInt()<<endl;
   }
   Json::Value array = root["arraycahr"];
   for(int i = 0;i < array.size();i++)
   {
	   std::cout<<array[i].asString()<<endl;
   }
   /*
   Json::Value obj1 = root["arrayobj1"];
   for(int i = 0;i < obj1.size();i++)
   {
	   std::cout<<obj1[i][].asString()<<endl;
   }
   */
    Json::Value obj2 = root["arrayobj2"];
   for(int i = 0;i < obj2.size();i++)
   {
	   std::cout<<obj2[i]["name"].asString()<<endl;
	   std::cout<<obj2[i]["age"].asInt()<<endl;
	   std::cout<<obj2[i]["weight"].asDouble()<<endl;
   }
}
}

2.3编译需要指定库

$  g++ jsoncpp.cpp -l jsoncpp  -o jsoncpp.out

3.1 jsoncpp构建json数据保存到json 文件

#include<string>
#include<iostream>
#include<fstream>
#include <jsoncpp/json/json.h> 
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<<array.size()<<endl;
	for (int index = 0; index < array.size(); ++index) {   // Iterates over the sequence elements.
        //cout << "Element " << index << " in array: " << array[index].asString() << endl;
	Json::Value position = array[index];
	cout << "Element: " << index << " x: " << position["x"].asFloat() <<"  y:"<< position["y"].asFloat() <<"  z:"<< position["z"].asFloat()<<"  w:"<< position["w"].asFloat()<< endl;
	position["x"]=Json::Value(1234);
	array[index] = position;
    }
}

root["array"] = array;
std::string str = fwriter.write(array);
std::ofstream ofs("example_fast_writer.json");
ofs << str;
ofs.close();

str = swriter.write(root);
ofs.open("example_styled_writer.json");
ofs << str;
ofs.close();
return 0;
}

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 <jsoncpp/json/json.h> 

3.1在CMakeList.txt文件 target_link_libraries(jsoncpp ${catkin_LIBRARIES}) 加入jsoncpp 

猜你喜欢

转载自blog.csdn.net/qq_29796781/article/details/80853300