目录
初始化
解析string字符串并输出
json%E8%B5%8B%E5%80%BCstring%EF%BC%8Cchar%20*%EF%BC%8CQString%EF%BC%8Cbool%EF%BC%8Cint-toc" style="margin-left:0px;">赋值 给json赋值string,char *,QString,bool,int
json%E8%BD%AC%E4%B8%BAstring%EF%BC%8Cchar%20*%EF%BC%8CQString%C2%A0-toc" style="margin-left:0px;">赋值 将json转为string,char *,QString
删除
嵌套对象和数组的组合与解析
JSON 数组 遍历,添加,修改
类型转换
类型检查
自定义类与 JSON 序列化
用的是nlohmann/json的库
初始化
#include <nlohmann/json.hpp>
using json = nlohmann::json;
解析string字符串并输出
std::string json_str = R"({"name": "John", "age": 30, "city": "New York"})";json j = json::parse(json_str);std::cout << "Name: " << j["name"] << "\n";
std::cout << "Age: " << j["age"] << "\n";
std::cout << "City: " << j["city"] << "\n";
json%E8%B5%8B%E5%80%BCstring%EF%BC%8Cchar%20*%EF%BC%8CQString%EF%BC%8Cbool%EF%BC%8Cint">赋值 给json赋值string,char *,QString,bool,int
json j;// 使用 std::string 给 JSON 赋值std::string name = "John";j["name"] = name;const char* city = "New York";j["city"] = city;QString country = "USA";j["country"] = country.toStdString();bool is_student = true;j["is_student"] = is_student;int age = 25;j["age"] = age;
json%E8%BD%AC%E4%B8%BAstring%EF%BC%8Cchar%20*%EF%BC%8CQString%C2%A0">赋值 将json转为string,char *,QString
json j;j["name"] = "John";j["age"] = 30;j["city"] = "New York";j["is_student"] = false;std::string json_str = j.dump();std::cout << json_str << std::endl;const char* char_text = json_str.c_str();// 如果需要修改 char*,可以进行拷贝:char* char_text_copy = new char[json_str.size() + 1];std::strcpy(char_text_copy, json_str.c_str());// 记得使用 delete[] 释放内存delete[] char_text_copy;QString qstr = QString::fromStdString(json_str);
删除
json j = {{"name", "John"},{"age", 30},{"city", "New York"}};j.erase("age");std::cout << j.dump(4) << std::endl;
嵌套对象和数组的组合与解析
json j1;// 嵌套 JSON 数组j1["name"] = "John";j1["age"] = 30;// 数组j1["phones"] = {"123-456-7890", "987-654-3210"};// 对象j1["address"] = {{"street", "123 Main St"}, {"city", "New York"}};
构建
{"address": {"city": "New York","street": "123 Main St"},"age": 30,"name": "John","phones": ["123-456-7890","987-654-3210"]
}json j = {{"name", "John"},{"age", 30},{"address", {{"street", "123 Main St"},{"city", "New York"},{"zipcode", "10001"}}},{"phones", {"123-456-7890", "987-654-3210"}}};// 访问嵌套数据std::cout << "Name: " << j["name"] << std::endl;std::cout << "Street: " << j["address"]["street"] << std::endl;std::cout << "First phone: " << j["phones"][0] << std::endl;
JSON 数组 遍历,添加,修改
// 遍历json j = {1, 2, 3, 4, 5};for (auto& el : j) {std::cout << el << " ";}std::cout << std::endl;// 添加j.push_back(6);// 输出修改后的数组std::cout << j.dump(4) << std::endl;// 修改json j = {{1, 2, 3},{4, 5, 6}};// 修改嵌套数组中的元素j[1][2] = 99;
[[1,2,3],[4,5,99]
]
类型转换
json j = {{"name", "John"},{"age", 30},{"is_student", false}};// 将 JSON 数据转换为 C++ 类型std::string name = j["name"].get<std::string>();int age = j["age"].get<int>();bool is_student = j["is_student"].get<bool>();std::cout << "Name: " << name << ", Age: " << age << ", Is student: " << is_student << std::endl;
类型检查
is_number_integer()is_string()is_array()is_object()is_boolean()
自定义类与 JSON 序列化
class Person {
public:std::string name;int age;bool is_student;// 自定义的序列化函数NLOHMANN_DEFINE_TYPE_INTRUSIVE(Person, name, age, is_student)
};// 创建 Person 对象Person p{"John", 30, false};// 将 Person 对象序列化为 JSONjson j = p;std::cout << j.dump(4) << std::endl;// 从 JSON 反序列化为 Person 对象Person new_p = j.get<Person>();std::cout << "Name: " << new_p.name << ", Age: " << new_p.age << ", Is student: " << new_p.is_student << std::endl;