C++中jsoncpp庫和nlohmann-json庫實現(xiàn)JSON與字符串類型轉(zhuǎn)換
在ROS中,可以使用jsoncpp
庫來實現(xiàn)JSON與字符串類型之間的轉(zhuǎn)換。jsoncpp
是ROS自帶的一個JSON庫,它提供了一些函數(shù)來解析和生成JSON數(shù)據(jù)。
下面是一個使用jsoncpp
庫實現(xiàn)JSON與字符串類型轉(zhuǎn)換的示例代碼:
#include <ros/ros.h> #include <jsoncpp/json/json.h> int main(int argc, char** argv) { // 初始化ROS節(jié)點 ros::init(argc, argv, "json_example"); ros::NodeHandle nh; // 創(chuàng)建一個JSON對象 Json::Value json; // 向JSON對象中添加數(shù)據(jù) json["name"] = "John"; json["age"] = 25; json["city"] = "New York"; // 將JSON對象轉(zhuǎn)換為字符串 std::string jsonString = json.toStyledString(); ROS_INFO("JSON string: %s", jsonString.c_str()); // 將字符串轉(zhuǎn)換為JSON對象 Json::Value parsedJson; Json::Reader reader; bool parsingSuccessful = reader.parse(jsonString, parsedJson); if (!parsingSuccessful) { ROS_ERROR("Failed to parse JSON string"); return 1; } // 從JSON對象中獲取數(shù)據(jù) std::string name = parsedJson["name"].asString(); int age = parsedJson["age"].asInt(); std::string city = parsedJson["city"].asString(); // 打印獲取的數(shù)據(jù) ROS_INFO("Name: %s", name.c_str()); ROS_INFO("Age: %d", age); ROS_INFO("City: %s", city.c_str()); return 0; }
在上面的示例代碼中,我們首先創(chuàng)建了一個Json::Value
對象,并向該對象中添加了一些數(shù)據(jù)。然后,我們使用toStyledString()
函數(shù)將JSON對象轉(zhuǎn)換為字符串,并使用Json::Reader
類的parse()
函數(shù)將字符串轉(zhuǎn)換為JSON對象。最后,我們從JSON對象中獲取數(shù)據(jù),并打印出來。
注意:在使用上述代碼之前,需要確保已經(jīng)安裝了jsoncpp
庫??梢允褂靡韵旅钤赗OS中安裝jsoncpp
庫:
sudo apt-get install ros-<distro>-jsoncpp
其中,<distro>
是ROS的發(fā)行版,如melodic
、noetic
等。Json::Value
和nlohmann::json
是兩個不同的JSON庫的數(shù)據(jù)類型。它們的使用方式略有不同。
Json::Value
是JsonCpp庫的數(shù)據(jù)類型,用于表示JSON數(shù)據(jù)。它的使用方式如下:
#include <jsoncpp/json/json.h> Json::Value data; // 從字符串解析JSON數(shù)據(jù) Json::Reader reader; std::string jsonString = "{\"key\": \"value\"}"; reader.parse(jsonString, data); // 訪問JSON數(shù)據(jù) std::string value = data["key"].asString(); std::cout << "Value: " << value << std::endl; // 修改JSON數(shù)據(jù) data["key"] = "new value"; // 將JSON數(shù)據(jù)轉(zhuǎn)換為字符串 Json::StyledWriter writer; std::string newJsonString = writer.write(data); std::cout << "New JSON String: " << newJsonString << std::endl;
nlohmann::json
是nlohmann-json庫的數(shù)據(jù)類型,也用于表示JSON數(shù)據(jù)。它的使用方式如下:
#include <nlohmann/json.hpp> nlohmann::json data; // 從字符串解析JSON數(shù)據(jù) std::string jsonString = "{\"key\": \"value\"}"; data = nlohmann::json::parse(jsonString); // 訪問JSON數(shù)據(jù) std::string value = data["key"].get<std::string>(); std::cout << "Value: " << value << std::endl; // 修改JSON數(shù)據(jù) data["key"] = "new value"; // 將JSON數(shù)據(jù)轉(zhuǎn)換為字符串 std::string newJsonString = data.dump(); std::cout << "New JSON String: " << newJsonString << std::endl;
注意,JsonCpp使用Json::Reader
和Json::StyledWriter
來解析和序列化JSON數(shù)據(jù),而nlohmann-json使用nlohmann::json::parse
和nlohmann::json::dump
來實現(xiàn)相同的功能。此外,JsonCpp庫需要包含jsoncpp/json/json.h
頭文件,而nlohmann-json庫需要包含nlohmann/json.hpp
頭文件。根據(jù)您使用的庫和個人喜好,選擇適合您的情況的庫和使用方式。下面是使用JsonCpp庫和nlohmann庫分別實現(xiàn)JSON和字符串之間轉(zhuǎn)換的示例代碼:
使用JsonCpp庫:
#include <iostream> #include <json/json.h> int main() { // 創(chuàng)建JSON對象 Json::Value jsonValue; jsonValue["name"] = "John"; jsonValue["age"] = 30; jsonValue["city"] = "New York"; // 將JSON對象轉(zhuǎn)換為字符串 Json::StreamWriterBuilder writer; std::string jsonString = Json::writeString(writer, jsonValue); std::cout << "JSON to string: " << jsonString << std::endl; // 將字符串轉(zhuǎn)換為JSON對象 Json::CharReaderBuilder reader; Json::Value parsedJson; std::istringstream jsonStringStream(jsonString); Json::parseFromStream(reader, jsonStringStream, &parsedJson, nullptr); // 從JSON對象中獲取數(shù)據(jù) std::string name = parsedJson["name"].asString(); int age = parsedJson["age"].asInt(); std::string city = parsedJson["city"].asString(); // 打印解析后的數(shù)據(jù) std::cout << "Parsed JSON:" << std::endl; std::cout << "Name: " << name << std::endl; std::cout << "Age: " << age << std::endl; std::cout << "City: " << city << std::endl; return 0; }
使用nlohmann庫:
#include <iostream> #include <nlohmann/json.hpp> int main() { // 創(chuàng)建JSON對象 nlohmann::json jsonValue; jsonValue["name"] = "John"; jsonValue["age"] = 30; jsonValue["city"] = "New York"; // 將JSON對象轉(zhuǎn)換為字符串 std::string jsonString = jsonValue.dump(); std::cout << "JSON to string: " << jsonString << std::endl; // 將字符串轉(zhuǎn)換為JSON對象 nlohmann::json parsedJson = nlohmann::json::parse(jsonString); // 從JSON對象中獲取數(shù)據(jù) std::string name = parsedJson["name"].get<std::string>(); int age = parsedJson["age"].get<int>(); std::string city = parsedJson["city"].get<std::string>(); // 打印解析后的數(shù)據(jù) std::cout << "Parsed JSON:" << std::endl; std::cout << "Name: " << name << std::endl; std::cout << "Age: " << age << std::endl; std::cout << "City: " << city << std::endl; return 0; }
在這兩個示例中,我們分別使用JsonCpp庫和nlohmann庫來創(chuàng)建JSON對象,并將其轉(zhuǎn)換為字符串。然后,我們將字符串解析為JSON對象,并從中提取數(shù)據(jù)。
請確保在編譯時鏈接JsonCpp庫或nlohmann庫,例如使用以下命令進行編譯:
使用JsonCpp庫:
g++ -o json_example json_example.cpp -ljsoncpp
使用nlohmann庫:
g++ -o json_example json_example.cpp -lnlohmann_json
這將生成一個名為json_example
的可執(zhí)行文件。運行此可執(zhí)行文件將輸出JSON轉(zhuǎn)換為字符串和字符串轉(zhuǎn)換為JSON的結(jié)果。
到此這篇關(guān)于jsoncpp庫和nlohmann-json庫實現(xiàn)JSON與字符串類型轉(zhuǎn)換的文章就介紹到這了,更多相關(guān)JSON與字符串類型轉(zhuǎn)換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C++實現(xiàn)utf8字符串和gbk字符串互轉(zhuǎn)
- C++實現(xiàn)判斷一個字符串是否為UTF8或GBK格式的方法
- C/C++實現(xiàn)數(shù)字與字符串互相轉(zhuǎn)換的多種方法
- 在C++中把字符串轉(zhuǎn)換為整數(shù)的兩種簡單方法
- C++實現(xiàn)將長整型數(shù)轉(zhuǎn)換為字符串的示例代碼
- C++實現(xiàn)十六進制字符串轉(zhuǎn)換成int整形值的示例
- c++中數(shù)字與字符串之間的轉(zhuǎn)換方法(推薦)
- C++實現(xiàn)十六進制字符串轉(zhuǎn)換為十進制整數(shù)的方法
- C++中utf8字符串和gbk字符串的轉(zhuǎn)換方法
相關(guān)文章
C++構(gòu)造函數(shù)深度學(xué)習(xí)
這篇文章主要為大家詳細介紹了C++構(gòu)造函數(shù),深度學(xué)習(xí)C++構(gòu)造函數(shù),感興趣的小伙伴們可以參考一下2016-08-08深入探討:main函數(shù)執(zhí)行完畢后,是否可能會再執(zhí)行一段代碼?
本篇文章是對main函數(shù)執(zhí)行完畢后,是否可能會再執(zhí)行一段代碼,進行了詳細的分析介紹,需要的朋友參考下2013-05-05C++實現(xiàn)簡單學(xué)生信息管理系統(tǒng)
這篇文章主要為大家詳細介紹了C++實現(xiàn)簡單學(xué)生信息管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03