json11 是 Dropbox 开发的一个轻量级 C++ JSON 解析库,提供简单易用的 API 和高性能的 JSON 解析能力。它是一个单头文件库,易于集成。
git clone https://github.com/dropbox/json11.git
cd json11
make
# 或者直接包含 json11.hpp
#include "json11.hpp"
#include "json11.hpp"
#include <iostream>
#include <string>
using namespace json11;
using namespace std;
int main() {
// 创建 JSON 对象
Json my_json = Json::object {
{ "name", "Alice" },
{ "age", 25 },
{ "active", true }
};
std::string json_str = my_json.dump();
std::cout << json_str << std::endl;
// 输出: {"name":"Alice","age":25,"active":true}
return 0;
}
#include "json11.hpp"
#include <iostream>
using namespace json11;
int main() {
// 创建 JSON 数组
Json my_array = Json::array {
"apple",
"banana",
"orange"
};
std::cout << my_array.dump() << std::endl;
// 输出: ["apple","banana","orange"]
// 嵌套对象和数组
Json nested = Json::object {
{ "users", Json::array {
Json::object { { "id", 1 }, { "name", "Alice" } },
Json::object { { "id", 2 }, { "name", "Bob" } }
}},
{ "count", 2 }
};
std::cout << nested.dump() << std::endl;
return 0;
}
#include "json11.hpp"
#include <iostream>
#include <string>
using namespace json11;
using namespace std;
int main() {
std::string json_str = R"(
{
"name": "Alice",
"age": 25,
"active": true,
"hobbies": ["reading", "coding", "gaming"]
}
)";
string err;
Json data = Json::parse(json_str, err);
if (!err.empty()) {
std::cerr << "Parse error: " << err << std::endl;
return 1;
}
// 访问字段
std::string name = data["name"].string_value();
int age = data["age"].int_value();
bool active = data["active"].bool_value();
std::cout << "Name: " << name << std::endl;
std::cout << "Age: " << age << std::endl;
std::cout << "Active: " << (active ? "Yes" : "No") << std::endl;
// 访问数组
Json hobbies = data["hobbies"];
std::cout << "Hobbies:" << std::endl;
for (const auto& hobby : hobbies.array_items()) {
std::cout << " - " << hobby.string_value() << std::endl;
}
return 0;
}
#include "json11.hpp"
#include <iostream>
using namespace json11;
int main() {
Json data = Json::object {
{ "str", "hello" },
{ "num", 42 },
{ "bool", true },
{ "null", Json::null },
{ "array", Json::array {1, 2, 3} }
};
// 检查类型
std::cout << "str is string: " << data["str"].is_string() << std::endl;
std::cout << "num is number: " << data["num"].is_number() << std::endl;
std::cout << "bool is bool: " << data["bool"].is_bool() << std::endl;
std::cout << "null is null: " << data["null"].is_null() << std::endl;
std::cout << "array is array: " << data["array"].is_array() << std::endl;
return 0;
}
#include "json11.hpp"
#include <iostream>
using namespace json11;
int main() {
Json data = Json::object {
{ "name", "Alice" },
{ "age", 25 }
};
// 安全访问,使用默认值
std::string name = data["name"].string_value();
std::string city = data["city"].string_value("Unknown");
int age = data["age"].int_value();
int score = data["score"].int_value(0);
std::cout << "Name: " << name << std::endl;
std::cout << "City: " << city << std::endl;
std::cout << "Age: " << age << std::endl;
std::cout << "Score: " << score << std::endl;
return 0;
}
#include "json11.hpp"
#include <iostream>
using namespace json11;
int main() {
Json data = Json::object {
{ "name", "Alice" },
{ "age", 25 },
{ "hobbies", Json::array {"reading", "coding"} }
};
// 压缩输出
std::cout << "Compact: " << data.dump() << std::endl;
// 格式化输出(带缩进)
std::cout << "Pretty:\n" << data.dump(Json::PRETTY_PRINT) << std::endl;
return 0;
}
#include "json11.hpp"
#include <iostream>
#include <fstream>
#include <string>
using namespace json11;
class Config {
public:
std::string name;
int port;
bool debug;
std::vector<std::string> hosts;
bool load(const std::string& filename) {
std::ifstream file(filename);
if (!file.is_open()) {
std::cerr << "Failed to open config file" << std::endl;
return false;
}
std::string content((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
file.close();
std::string err;
Json json = Json::parse(content, err);
if (!err.empty()) {
std::cerr << "Parse error: " << err << std::endl;
return false;
}
name = json["name"].string_value("app");
port = json["port"].int_value(8080);
debug = json["debug"].bool_value(false);
for (const auto& host : json["hosts"].array_items()) {
hosts.push_back(host.string_value());
}
return true;
}
bool save(const std::string& filename) const {
Json json = Json::object {
{ "name", name },
{ "port", port },
{ "debug", debug },
{ "hosts", hosts }
};
std::ofstream file(filename);
if (!file.is_open()) {
return false;
}
file << json.dump(Json::PRETTY_PRINT);
file.close();
return true;
}
void print() const {
std::cout << "Config:" << std::endl;
std::cout << " Name: " << name << std::endl;
std::cout << " Port: " << port << std::endl;
std::cout << " Debug: " << (debug ? "Yes" : "No") << std::endl;
std::cout << " Hosts:" << std::endl;
for (const auto& host : hosts) {
std::cout << " - " << host << std::endl;
}
}
};
int main() {
Config config;
config.name = "MyApp";
config.port = 9000;
config.debug = true;
config.hosts = {"localhost", "127.0.0.1"};
// 保存配置
if (config.save("config.json")) {
std::cout << "Config saved" << std::endl;
}
// 加载配置
Config loaded;
if (loaded.load("config.json")) {
loaded.print();
}
return 0;
}
| 类型 | 判断函数 | 获取函数 |
|---|---|---|
| 字符串 | is_string() |
string_value() |
| 数字 | is_number() |
int_value(), number_value() |
| 布尔 | is_bool() |
bool_value() |
| 数组 | is_array() |
array_items() |
| 对象 | is_object() |
object_items() |
| 空值 | is_null() |
Json::null |