➕ JSON 转 C++ 结构体JSON to C++ Struct
把 JSON 转换成 C++ 结构体:std::string、std::vector、std::optional 等现代 C++17 类型,嵌套结构按依赖顺序输出,可选生成 nlohmann/json 的 from_json / to_json 序列化函数。Turn JSON into C++ structs using modern C++17 types (std::string, std::vector, std::optional), ordered by dependency, with optional nlohmann/json from_json / to_json functions.
使用说明与常见问题How-to & FAQ
「JSON 转 C++ 结构体」是什么?What is the JSON to C++ Struct tool?
把 JSON 转换成 C++ 结构体:std::string、std::vector、std::optional 等现代 C++17 类型,嵌套结构按依赖顺序输出,可选生成 nlohmann/json 的 from_json / to_json 序列化函数。Turn JSON into C++ structs using modern C++17 types (std::string, std::vector, std::optional), ordered by dependency, with optional nlohmann/json from_json / to_json functions.
如何使用「JSON 转 C++ 结构体」?How do I use the JSON to C++ Struct?
粘贴 JSON 并填写根结构体名,下方生成可直接编译的头文件。可能缺失或为 null 的字段会用 std::optional;勾选 nlohmann/json 后还会生成序列化函数,直接 j.get
生成的代码需要什么编译器?Which compiler standard is needed?
代码使用 std::optional 和 if 初始化语句,需要 C++17 或更高(如 g++ -std=c++17)。nlohmann/json 是单头文件库,可以通过 vcpkg、Conan 或直接下载 json.hpp 引入。The code uses std::optional and if-with-initializer, so it needs C++17 or later (e.g. g++ -std=c++17). nlohmann/json is a single-header library available via vcpkg, Conan or by downloading json.hpp.
为什么结构体顺序和 JSON 里不一样?Why is the struct order different?
C++ 要求先定义被使用的类型,所以嵌套的子结构体会排在父结构体前面输出。C++ requires a type to be defined before it is used by value, so nested structs are emitted before their parents.
「JSON 转 C++ 结构体」免费吗?数据会上传吗?Is the JSON to C++ Struct free? Is my data uploaded?
「JSON 转 C++ 结构体」完全免费,无需注册或安装,手机和电脑都能用;所有处理都在你的浏览器本地完成,输入的内容和文件不会上传到服务器。The JSON to C++ Struct is completely free with no sign-up or install and works on phones and desktops; everything runs locally in your browser and nothing you enter is uploaded.