JSON是一種輕量級資料交換格式,易於閱讀和編寫,也易於機器解析和產生。使用JSON格式可以方便地在各個系統之間傳遞資料。在C 中,有許多開源的JSON函式庫可以進行JSON的處理。本文將介紹一些常用的C 中的JSON處理方法及實作。
C 中的JSON處理方法
RapidJSON是一個快速的C JSON解析器/產生器,提供DOM、SAX和內存池樣式API。它主要的特點有以下幾點:
RapidJSON中,JSON物件可以透過DOM和SAX方式解析,其中DOM方式可以透過Value類別來實現。以下是一個使用RapidJSON產生並解析JSON的範例程式碼:
#include "rapidjson/document.h" #include "rapidjson/writer.h" #include "rapidjson/stringbuffer.h" #include <iostream> using namespace rapidjson; using namespace std; int main() { // 生成JSON StringBuffer s; Writer<StringBuffer> writer(s); writer.StartObject(); writer.Key("name"); writer.String("Tom"); writer.Key("age"); writer.Int(20); writer.EndObject(); // 解析JSON Document d; d.Parse(s.GetString()); cout << "name: " << d["name"].GetString() << endl; cout << "age: " << d["age"].GetInt() << endl; return 0; }
Boost.PropertyTree是一個簡單易用的屬性處理函式庫,能夠處理各種屬性格式。其中,它也支援解析和生成JSON。 Boost.PropertyTree比RapidJSON略慢,但也有一些特點:
#include <iostream> #include <boost/property_tree/ptree.hpp> #include <boost/property_tree/json_parser.hpp> using namespace std; using boost::property_tree::ptree; using boost::property_tree::read_json; using boost::property_tree::write_json; int main() { // 生成JSON ptree pt; pt.put("name", "Tom"); pt.put("age", 20); // 解析JSON string json_str; write_json(cout, pt); cout << endl; read_json("test.json", pt); cout << "name: " << pt.get<string>("name") << endl; cout << "age: " << pt.get<int>("age") << endl; return 0; }
#include <iostream> #include <json/json.h> using namespace std; using namespace Json; int main() { // 生成JSON Value root; root["name"] = "Tom"; root["age"] = 20; string json_str = root.toStyledString(); cout << json_str << endl; // 解析JSON Reader reader; Value value; reader.parse("{"name":"Tom","age":20}", value, false); cout << "name: " << value["name"].asString() << endl; cout << "age: " << value["age"].asInt() << endl; return 0; }
#include <iostream> #include <nlohmann/json.hpp> using namespace std; using json = nlohmann::json; int main() { // 生成JSON json j; j["name"] = "Tom"; j["age"] = 20; string json_str = j.dump(); cout << json_str << endl; // 解析JSON json j2 = json::parse("{"name":"Tom","age":20}"); cout << "name: " << j2["name"] << endl; cout << "age: " << j2["age"] << endl; return 0; }
// 生成JSON Value root(kObjectType); Value person(kObjectType); person.AddMember("name", "Tom", allocator); person.AddMember("age", 20, allocator); root.AddMember("person", person, allocator); StringBuffer buffer; Writer<StringBuffer> writer(buffer); root.Accept(writer); cout << buffer.GetString() << endl;
// 解析JSON Document d; d.Parse("{"person":{"name":"Tom","age":20}}"); const Value& person = d["person"]; const string name = person["name"].GetString(); const int age = person["age"].GetInt();
// 生成JSON ptree root; ptree person; person.put("name", "Tom"); person.put("age", 20); root.add_child("person", person); stringstream stream; write_json(stream, root); cout << stream.str() << endl;
// 解析JSON ptree root; read_json("test.json", root); const string name = root.get<string>("person.name"); const int age = root.get<int>("person.age");
// 生成JSON Value root; Value person; person["name"] = "Tom"; person["age"] = 20; root["person"] = person; cout << root.toStyledString() << endl;
// 解析JSON Reader reader; Value value; string json_str = "{"person":{"name":"Tom","age":20}}"; reader.parse(json_str, value); const string name = value["person"]["name"].asString(); const int age = value["person"]["age"].asInt();
// 生成JSON json j; j["person"]["name"] = "Tom"; j["person"]["age"] = 20; cout << j.dump() << endl;
// 解析JSON json j2 = json::parse("{"person":{"name":"Tom","age":20}}"); const string name = j2["person"]["name"]; const int age = j2["person"]["age"];
以上是C++中的JSON處理方法及實現的詳細內容。更多資訊請關注PHP中文網其他相關文章!