一、C++ JSON解析方法
JSON(JavaScript Object Notation)是一種輕量級的數據交換格式。C++ JSON解析器可以將JSON格式的字符串轉換為C++對象並操作C++對象。C++11引入了一個名為json的類,它提供了用於解析和生成JSON文本的API。
#include <iostream> #include <nlohmann/json.hpp> #include <string> using json = nlohmann::json; // 引入 nlohmann/json 庫 int main() { std::string str = "{\"name\": \"Jack\", \"age\": 20}"; auto j = json::parse(str); // 解析JSON std::cout << "name: " << j["name"] << ", age: " << j["age"] << std::endl; // 輸出結果 return 0; }
二、CJSON解析JSON數組
CJSON是一個輕量級的C庫,用於解析JSON數據。該庫提供了解析JSON格式的函數,支持解析JSON數組。
#include <stdio.h> #include <cjson/cJSON.h> int main () { char *jsonstr = "[{\"name\": \"Jack\", \"age\": 20}, {\"name\": \"Mike\", \"age\": 30}]"; cJSON *root = cJSON_Parse(jsonstr); // 解析JSON cJSON *person = NULL; cJSON_ArrayForEach(person, root) { // 遍歷JSON數組 printf("name: %s, age: %d\n", cJSON_GetObjectItem(person, "name")->valuestring, cJSON_GetObjectItem(person, "age")->valueint); } cJSON_Delete(root); // 釋放內存 return 0; }
三、C++ JSON解析庫
除了C++11自帶的json類外,還有許多流行的C++ JSON解析庫,例如RapidJSON、JSON++、nlohmann/json等。這些庫以其高效的解析速度和易於使用的API而受到歡迎。
#include <iostream> #include <rapidjson/document.h> #include <string> using namespace rapidjson; // 引入 rapidjson 庫 int main() { std::string str = "{\"name\": \"Jack\", \"age\": 20}"; Document d; d.Parse(str.c_str()); // 解析JSON std::cout << "name: " << d["name"].GetString() << ", age: " << d["age"].GetInt() << std::endl; // 輸出結果 return 0; }
四、JSON解析db文件
在C++中,可以使用jsoncpp庫來解析和生成JSON格式的文件。該庫提供了一個名為Json::Value的類,該類可以表示JSON值。
#include <iostream> #include <fstream> #include <jsoncpp/json/json.h> int main() { std::ifstream file("data.json"); Json::Reader reader; Json::Value root; reader.parse(file, root); // 解析JSON文件 std::cout << "name: " << root["name"].asString() << ", age: " << root["age"].asInt() << std::endl; // 輸出結果 return 0; }
五、JSON解析
JSON是通用的數據交換格式,可以用於不同語言之間的數據交換。在C++中,我們可以通過各種JSON解析庫將JSON格式的數據轉換為C++對象或數據結構,同樣,我們也可以將C++對象或數據結構序列化為JSON格式的數據。
六、PHP解析JSON
在PHP中,可以使用json_decode()函數來將JSON格式的數據解碼為PHP對象或數組。json_decode()函數可以接受一個可選的第二個參數,指定解碼後生成的對象類型,默認是stdClass。
$jsonstr = '{"name": "Jack", "age": 20}'; $json = json_decode($jsonstr); echo "name: " . $json->name . ", age: " . $json->age; // 輸出結果
七、C解析JSON數組
在C語言中,我們可以使用cJSON庫來解析JSON格式的數據。該庫提供了解析JSON格式的函數,支持解析JSON數組。
#include <stdio.h> #include <cJSON/cJSON.h> int main () { char *jsonstr = "[{\"name\": \"Jack\", \"age\": 20}, {\"name\": \"Mike\", \"age\": 30}]"; cJSON *root = cJSON_Parse(jsonstr); // 解析JSON cJSON *person = NULL; cJSON_ArrayForEach(person, root) { // 遍歷JSON數組 printf("name: %s, age: %d\n", cJSON_GetObjectItem(person, "name")->valuestring, cJSON_GetObjectItem(person, "age")->valueint); } cJSON_Delete(root); // 釋放內存 return 0; }
八、JSON解析文件
JSON是一種常用的數據格式,在C++中,我們可以使用許多庫來解析和生成JSON格式的文件。這些庫提供了易於使用的API,可以快速解析和生成JSON格式的文件。
#include <iostream> #include <fstream> #include <nlohmann/json.hpp> using json = nlohmann::json; // 引入 nlohmann/json 庫 int main() { std::ifstream file("data.json"); json j; file >> j; // 解析JSON文件 std::cout << "name: " << j["name"] << ", age: " << j["age"] << std::endl; // 輸出結果 return 0; }
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/241193.html