0

0

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

冰火之心

冰火之心

发布时间:2025-09-21 16:06:01

|

232人浏览过

|

来源于php中文网

原创

使用nlohmann/json库可高效解析JSON到std::map,其头文件设计、C++风格API及类型安全特性使其成为首选;通过std::map可灵活处理嵌套结构,而数组宜用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
的结构,或者更灵活地映射到它自己的
json
类型,从而处理各种复杂数据。

解决方案

使用

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

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

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

#include 
#include 
#include 
#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
        // 注意:这种直接转换只适用于JSON对象的所有值都是字符串的情况。
        // 如果值类型不一致,或者有嵌套结构,你需要更细致的处理。
        std::map 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::cout << "Parsed into std::map:" << 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
        // 这种方式可以保留原始JSON的类型和结构,包括嵌套对象和数组。
        std::map generic_map = j.get>();

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

        // 4. 从std::map中获取特定类型的值
        if (generic_map.count("age") && generic_map["age"].is_number_integer()) {
            int age = generic_map["age"].get();
            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
,这更灵活,能处理异构和嵌套的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()
提取值。这种设计在处理结构不那么固定的JSON时尤其有用。

当然,也有像

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

海螺语音
海螺语音

海螺AI推出的AI语音生成工具,支持多种语种、情绪和效果。

下载

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

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

std::map
就不现实了。这时候,我们的策略需要升级。

核心思路是利用

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

#include 
#include 
#include 
#include 
#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
        // 这是处理复杂JSON最灵活的起点
        std::map root_map = j.get>();

        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 user_info_map = root_map["user_info"].get>();
            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::endl;
            }
            if (user_info_map.count("preferences") && user_info_map["preferences"].is_object()) {
                std::map prefs_map = user_info_map["preferences"].get>();
                std::cout << "  Preferences Theme: " << prefs_map["theme"].get() << 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 product_map = product_item.get>();
                std::cout << "  Product ID: " << product_map["product_id"].get()
                          << ", Quantity: " << product_map["quantity"].get() << 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
,我们就可以逐层深入,检查每个
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 
#include 
#include 
#include 
#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 j_array = json::parse(array_json_string);
        std::vector items_vector = j_array.get>();

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

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

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

        std::cout << "Parsed mixed JSON array into std::vector:" << 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
可以是
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 
#include 
#include 
#include 
#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 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();
                    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::endl;
        }
        if (categories_by_id.count("electronics")) {
            std::cout << "  Electronics Item Count: " << categories_by_id["electronics"]["count"].get() << 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) {

相关专题

更多
json数据格式
json数据格式

JSON是一种轻量级的数据交换格式。本专题为大家带来json数据格式相关文章,帮助大家解决问题。

400

2023.08.07

json是什么
json是什么

JSON是一种轻量级的数据交换格式,具有简洁、易读、跨平台和语言的特点,JSON数据是通过键值对的方式进行组织,其中键是字符串,值可以是字符串、数值、布尔值、数组、对象或者null,在Web开发、数据交换和配置文件等方面得到广泛应用。本专题为大家提供json相关的文章、下载、课程内容,供大家免费下载体验。

528

2023.08.23

jquery怎么操作json
jquery怎么操作json

操作的方法有:1、“$.parseJSON(jsonString)”2、“$.getJSON(url, data, success)”;3、“$.each(obj, callback)”;4、“$.ajax()”。更多jquery怎么操作json的详细内容,可以访问本专题下面的文章。

305

2023.10.13

go语言处理json数据方法
go语言处理json数据方法

本专题整合了go语言中处理json数据方法,阅读专题下面的文章了解更多详细内容。

67

2025.09.10

string转int
string转int

在编程中,我们经常会遇到需要将字符串(str)转换为整数(int)的情况。这可能是因为我们需要对字符串进行数值计算,或者需要将用户输入的字符串转换为整数进行处理。php中文网给大家带来了相关的教程以及文章,欢迎大家前来学习阅读。

311

2023.08.02

java中boolean的用法
java中boolean的用法

在Java中,boolean是一种基本数据类型,它只有两个可能的值:true和false。boolean类型经常用于条件测试,比如进行比较或者检查某个条件是否满足。想了解更多java中boolean的相关内容,可以阅读本专题下面的文章。

342

2023.11.13

java boolean类型
java boolean类型

本专题整合了java中boolean类型相关教程,阅读专题下面的文章了解更多详细内容。

19

2025.11.30

c语言中null和NULL的区别
c语言中null和NULL的区别

c语言中null和NULL的区别是:null是C语言中的一个宏定义,通常用来表示一个空指针,可以用于初始化指针变量,或者在条件语句中判断指针是否为空;NULL是C语言中的一个预定义常量,通常用来表示一个空值,用于表示一个空的指针、空的指针数组或者空的结构体指针。

226

2023.09.22

苹果官网入口直接访问
苹果官网入口直接访问

苹果官网直接访问入口是https://www.apple.com/cn/,该页面具备0.8秒首屏渲染、HTTP/3与Brotli加速、WebP+AVIF双格式图片、免登录浏览全参数等特性。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

6

2025.12.24

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
WEB前端教程【HTML5+CSS3+JS】
WEB前端教程【HTML5+CSS3+JS】

共101课时 | 7.9万人学习

JS进阶与BootStrap学习
JS进阶与BootStrap学习

共39课时 | 3万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

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