ROS 使用C++ jsoncpp庫 讀寫儲存json.json 檔案
阿新 • • 發佈:2019-02-20
我使用的環境之 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