首页 > 后端开发 > C++ > 正文

如何在C++中将JSON解析为map_C++ JSON解析库应用实践

冰火之心
发布: 2025-09-21 16:06:01
原创
217人浏览过
使用nlohmann/json库可高效解析JSON到std::map,其头文件设计、C++风格API及类型安全特性使其成为首选;通过std::map<std::string, json>可灵活处理嵌套结构,而数组宜用std::vector,必要时可按键值转为std::map以实现快速查找。

如何在c++中将json解析为map_c++ json解析库应用实践

在C++中将JSON解析为

std::map
登录后复制
,最直接且现代化的做法是利用一个成熟的JSON解析库,例如
nlohmann/json
登录后复制
。这个库以其C++11/14/17的风格和易用性,让JSON数据与C++容器之间的转换变得非常自然。它能将JSON对象直接映射到类似
std::map<std::string, T>
登录后复制
的结构,或者更灵活地映射到它自己的
json
登录后复制
类型,从而处理各种复杂数据。

解决方案

使用

nlohmann/json
登录后复制
库来解析JSON字符串并将其内容提取到
std::map
登录后复制
中。

首先,你需要确保项目中包含了

nlohmann/json
登录后复制
库。它是一个头文件库,通常只需要将其
json.hpp
登录后复制
文件包含到你的项目中即可。

#include <iostream>
#include <string>
#include <map>
#include "json.hpp" // 假设json.hpp在你的include路径中

// 为了方便,使用nlohmann::json的别名
using json = nlohmann::json;

int main() {
    std::string json_string = R"({
        "name": "Alice",
        "age": 30,
        "city": "New York",
        "occupation": "Engineer"
    })";

    try {
        // 1. 解析JSON字符串
        json j = json::parse(json_string);

        // 2. 将JSON对象的内容提取到std::map<std::string, std::string>
        // 注意:这种直接转换只适用于JSON对象的所有值都是字符串的情况。
        // 如果值类型不一致,或者有嵌套结构,你需要更细致的处理。
        std::map<std::string, std::string> string_map;
        for (json::iterator it = j.begin(); it != j.end(); ++it) {
            // 尝试将值转换为字符串。如果值不是字符串类型,这里可能会抛出异常
            // 或者进行隐式转换(如果nlohmann/json支持)。
            // 更安全的做法是先检查类型:if (it.value().is_string())
            string_map[it.key()] = it.value().get<std::string>();
        }

        std::cout << "Parsed into std::map<std::string, std::string>:" << std::endl;
        for (const auto& pair : string_map) {
            std::cout << "  " << pair.first << ": " << pair.second << std::endl;
        }

        std::cout << "\n----------------------------------\n" << std::endl;

        // 3. 更通用的方法:将JSON对象转换为std::map<std::string, json>
        // 这种方式可以保留原始JSON的类型和结构,包括嵌套对象和数组。
        std::map<std::string, json> generic_map = j.get<std::map<std::string, json>>();

        std::cout << "Parsed into std::map<std::string, nlohmann::json>:" << std::endl;
        for (const auto& pair : generic_map) {
            std::cout << "  " << pair.first << ": " << pair.second.dump() << std::endl;
        }

        // 4. 从std::map<std::string, json>中获取特定类型的值
        if (generic_map.count("age") && generic_map["age"].is_number_integer()) {
            int age = generic_map["age"].get<int>();
            std::cout << "\nAge from generic_map: " << age << std::endl;
        }

    } catch (const json::parse_error& e) {
        std::cerr << "JSON parsing error: " << e.what() << std::endl;
    } catch (const json::type_error& e) {
        std::cerr << "JSON type error during conversion: " << e.what() << std::endl;
    } catch (const std::exception& e) {
        std::cerr << "An unexpected error occurred: " << e.what() << std::endl;
    }

    return 0;
}
登录后复制

这段代码展示了两种主要的转换方式:一种是直接尝试将所有值转换为特定类型(如

std::string
登录后复制
),这要求JSON结构非常规整;另一种是将其转换为
std::map<std::string, json>
登录后复制
,这更灵活,能处理异构和嵌套的JSON数据,之后再根据需要从
nlohmann::json
登录后复制
对象中提取具体类型的值。

立即学习C++免费学习笔记(深入)”;

为什么
nlohmann/json
登录后复制
是C++ JSON解析的首选之一?它的优势在哪里?

说实话,我个人觉得

nlohmann/json
登录后复制
这个库简直是C++处理JSON的“瑞士军刀”。它的优势非常明显,尤其是对于那些不想为了JSON解析而写一大堆模板代码的开发者来说。

首先,它是一个头文件库。这意味着你不需要编译额外的库文件,直接把

json.hpp
登录后复制
扔到你的项目里,
#include
登录后复制
一下就能用,这大大简化了构建流程,尤其是在跨平台或者CI/CD环境中,省去了不少麻烦。

其次,它的API设计非常C++-idiomatic。它充分利用了C++11及更高版本的特性,比如范围for循环、初始化列表、隐式类型转换等,让操作JSON对象感觉就像在操作

std::map
登录后复制
std::vector
登录后复制
一样自然。你可以用
[]
登录后复制
操作符访问元素,用
.at()
登录后复制
安全访问,用
.dump()
登录后复制
序列化,甚至直接将JSON对象赋值给C++结构体(通过
from_json
登录后复制
to_json
登录后复制
)。这种“感觉对了”的编程体验,能显著提升开发效率和代码可读性

再来,类型安全与灵活性兼顾

nlohmann/json
登录后复制
内部的
json
登录后复制
类型是一个变体(variant),可以存储JSON支持的所有基本类型(null、boolean、number、string、array、object)。这意味着你可以用一个
json
登录后复制
对象来表示任何JSON值,并在运行时通过
.is_string()
登录后复制
,
.is_number()
登录后复制
等方法检查其类型,然后安全地使用
.get<T>()
登录后复制
提取值。这种设计在处理结构不那么固定的JSON时尤其有用。

当然,也有像

RapidJSON
登录后复制
这样以极致性能著称的库。
RapidJSON
登录后复制
在解析大型文件或对性能有严苛要求时确实有优势,因为它采用SAX解析器和更底层的内存管理。但它的API相对来说就没那么“C++友好”,需要更多手动处理,代码量也会增加。对于大多数日常应用,
nlohmann/json
登录后复制
的性能已经绰绰有余了,而它带来的开发便利性是无与伦比的。所以,如果你不是在做那种每秒要处理几十GB JSON的系统,
nlohmann/json
登录后复制
通常是更好的选择。

幻舟AI
幻舟AI

专为短片创作者打造的AI创作平台

幻舟AI 279
查看详情 幻舟AI

处理复杂或嵌套JSON结构到
std::map
登录后复制
的策略是什么?

当JSON结构开始变得复杂,比如有嵌套对象或数组时,直接将其解析到简单的

std::map<std::string, std::string>
登录后复制
就不现实了。这时候,我们的策略需要升级。

核心思路是利用

nlohmann::json
登录后复制
本身的灵活性,将嵌套结构先解析为
std::map<std::string, nlohmann::json>
登录后复制
。这样,每个值(
nlohmann::json
登录后复制
类型)都可以继续代表一个子对象、一个数组或一个基本类型。

#include <iostream>
#include <string>
#include <map>
#include <vector>
#include "json.hpp"

using json = nlohmann::json;

int main() {
    std::string complex_json_string = R"({
        "user_info": {
            "id": 123,
            "name": "Bob",
            "email": "bob@example.com",
            "preferences": {
                "theme": "dark",
                "notifications": true
            }
        },
        "products_bought": [
            {"product_id": "A1", "quantity": 2},
            {"product_id": "B3", "quantity": 1}
        ],
        "is_active": true
    })";

    try {
        json j = json::parse(complex_json_string);

        // 策略1: 将整个JSON解析为std::map<std::string, json>
        // 这是处理复杂JSON最灵活的起点
        std::map<std::string, json> root_map = j.get<std::map<std::string, json>>();

        std::cout << "Root map keys and their JSON values:" << std::endl;
        for (const auto& pair : root_map) {
            std::cout << "  Key: " << pair.first << ", Value: " << pair.second.dump() << std::endl;
        }

        // 策略2: 访问嵌套对象并将其解析为另一个std::map
        if (root_map.count("user_info") && root_map["user_info"].is_object()) {
            std::map<std::string, json> user_info_map = root_map["user_info"].get<std::map<std::string, json>>();
            std::cout << "\nUser Info Map:" << std::endl;
            if (user_info_map.count("name") && user_info_map["name"].is_string()) {
                std::cout << "  Name: " << user_info_map["name"].get<std::string>() << std::endl;
            }
            if (user_info_map.count("preferences") && user_info_map["preferences"].is_object()) {
                std::map<std::string, json> prefs_map = user_info_map["preferences"].get<std::map<std::string, json>>();
                std::cout << "  Preferences Theme: " << prefs_map["theme"].get<std::string>() << std::endl;
            }
        }

        // 策略3: 遍历JSON数组
        if (root_map.count("products_bought") && root_map["products_bought"].is_array()) {
            json products_array = root_map["products_bought"];
            std::cout << "\nProducts Bought:" << std::endl;
            for (const auto& product_item : products_array) {
                // 每个数组元素都是一个JSON对象,可以再次解析为map
                std::map<std::string, json> product_map = product_item.get<std::map<std::string, json>>();
                std::cout << "  Product ID: " << product_map["product_id"].get<std::string>()
                          << ", Quantity: " << product_map["quantity"].get<int>() << std::endl;
            }
        }

    } catch (const json::parse_error& e) {
        std::cerr << "JSON parsing error: " << e.what() << std::endl;
    } catch (const json::type_error& e) {
        std::cerr << "JSON type error during conversion: " << e.what() << std::endl;
    } catch (const std::exception& e) {
        std::cerr << "An unexpected error occurred: " << e.what() << std::endl;
    }

    return 0;
}
登录后复制

通过将外部对象解析到

std::map<std::string, json>
登录后复制
,我们就可以逐层深入,检查每个
json
登录后复制
元素的类型,然后根据需要将其转换为更具体的C++类型(如
int
登录后复制
,
std::string
登录后复制
,
bool
登录后复制
),或者再次解析为嵌套的
std::map
登录后复制
std::vector
登录后复制
。这种层层递进的方式,虽然看起来有点繁琐,但它非常健壮,能确保你在处理未知或半结构化JSON时不会因为类型不匹配而崩溃。

对于更复杂的、结构固定的JSON,你还可以定义C++结构体,并使用

nlohmann/json
登录后复制
from_json
登录后复制
to_json
登录后复制
函数进行自定义序列化/反序列化。这样,整个JSON对象就可以直接映射到一个C++结构体实例,内部的嵌套对象和数组也会自动映射到结构体成员,代码会变得非常简洁和类型安全。但这就超出了单纯解析到
std::map
登录后复制
的范畴了。

将JSON数组解析到
std::vector
登录后复制
std::map
登录后复制
中有什么不同?

JSON数组和C++的

std::vector
登录后复制
简直是天作之合。它们在结构上高度相似:都是有序的、同类型或异类型元素的集合。所以,将JSON数组解析到
std::vector
登录后复制
通常是首选,也是最自然的方式。

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include "json.hpp"

using json = nlohmann::json;

int main() {
    std::string array_json_string = R"([
        {"id": 1, "name": "Item A"},
        {"id": 2, "name": "Item B", "status": "active"},
        {"id": 3, "name": "Item C"}
    ])";

    std::string mixed_array_json_string = R"([
        "apple",
        123,
        true,
        {"color": "red"}
    ])";

    try {
        // 解析数组到std::vector<json>
        json j_array = json::parse(array_json_string);
        std::vector<json> items_vector = j_array.get<std::vector<json>>();

        std::cout << "Parsed JSON array into std::vector<nlohmann::json>:" << std::endl;
        for (const auto& item_json : items_vector) {
            // 每个item_json本身又是一个JSON对象,可以进一步解析到map
            std::map<std::string, json> item_map = item_json.get<std::map<std::string, json>>();
            std::cout << "  ID: " << item_map["id"].get<int>()
                      << ", Name: " << item_map["name"].get<std::string>();
            if (item_map.count("status")) { // 检查可选字段
                std::cout << ", Status: " << item_map["status"].get<std::string>();
            }
            std::cout << std::endl;
        }

        std::cout << "\n----------------------------------\n" << std::endl;

        // 解析混合类型数组
        json j_mixed_array = json::parse(mixed_array_json_string);
        std::vector<json> mixed_items_vector = j_mixed_array.get<std::vector<json>>();

        std::cout << "Parsed mixed JSON array into std::vector<nlohmann::json>:" << std::endl;
        for (const auto& item : mixed_items_vector) {
            std::cout << "  Item (type: " << item.type_name() << "): " << item.dump() << std::endl;
        }

    } catch (const json::parse_error& e) {
        std::cerr << "JSON parsing error: " << e.what() << std::endl;
    } catch (const json::type_error& e) {
        std::cerr << "JSON type error during conversion: " << e.what() << std::endl;
    } catch (const std::exception& e) {
        std::cerr << "An unexpected error occurred: " << e.what() << std::endl;
    }

    return 0;
}
登录后复制

如你所见,

nlohmann/json
登录后复制
能够轻松地将JSON数组直接转换为
std::vector<T>
登录后复制
,其中
T
登录后复制
可以是
int
登录后复制
,
std::string
登录后复制
,
bool
登录后复制
,甚至是
nlohmann::json
登录后复制
本身,后者在处理异构数组或数组中包含对象时非常有用。

那么,什么时候会把JSON数组解析到

std::map
登录后复制
中呢?这通常不是直接的数组到
map
登录后复制
的转换,而是当你的JSON结构是这样的:

{
  "categories": [
    {"id": "electronics", "items": [...]},
    {"id": "books", "items": [...]}
  ]
}
登录后复制

在这种情况下,

categories
登录后复制
本身是一个数组,但你可能希望根据
id
登录后复制
来快速查找某个类别。那么,你可以先将整个
categories
登录后复制
数组解析出来,然后手动遍历这个数组,将每个元素的
id
登录后复制
作为
map
登录后复制
的键,将整个元素(或其
items
登录后复制
数组)作为
map
登录后复制
的值。

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include "json.hpp"

using json = nlohmann::json;

int main() {
    std::string categories_json_string = R"({
        "categories": [
            {"id": "electronics", "name": "Electronics Devices", "count": 120},
            {"id": "books", "name": "Books & Literature", "count": 350},
            {"id": "clothing", "name": "Apparel", "count": 200}
        ]
    })";

    try {
        json j_root = json::parse(categories_json_string);

        // 目标:将categories数组转换为map,键是id,值是整个类别对象
        std::map<std::string, json> categories_by_id;

        if (j_root.count("categories") && j_root["categories"].is_array()) {
            for (const auto& category_json : j_root["categories"]) {
                if (category_json.is_object() && category_json.count("id") && category_json["id"].is_string()) {
                    std::string category_id = category_json["id"].get<std::string>();
                    categories_by_id[category_id] = category_json; // 将整个JSON对象作为值
                }
            }
        }

        std::cout << "Categories mapped by ID:" << std::endl;
        if (categories_by_id.count("books")) {
            std::cout << "  Books Category Name: " << categories_by_id["books"]["name"].get<std::string>() << std::endl;
        }
        if (categories_by_id.count("electronics")) {
            std::cout << "  Electronics Item Count: " << categories_by_id["electronics"]["count"].get<int>() << std::endl;
        }

    } catch (const json::parse_error& e) {
        std::cerr << "JSON parsing error: " << e.what() << std::endl;
    } catch (const json::type_error& e) {
        std::cerr << "JSON type error during conversion: " << e.what() << std::endl;
    } catch (const std::exception& e) {
登录后复制

以上就是如何在C++中将JSON解析为map_C++ JSON解析库应用实践的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号