• C++使用nlohmann/json 转 C++ 结构体


    参考网址:https://icode.best/i/33144046003522

    #include "json.hpp"
    using namespace std;
    using namespace nlohmann;
    
    struct JsonData
    {
    	string path;
    	int age;
    };
    
    /*NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(JsonData,
    	path,
    	age)*/
    
    void from_json(const json& j, JsonData& p)
    {
    	j.at("path").get_to(p.path);
    	j.at("age").get_to(p.age);
    }
    
    int main()
    {
    	vector<int> s;
    	s.push_back(1);
    	s.push_back(2);
    	s.push_back(3);
    
    	int s1 = s.back();
    	int s2 = 1;
    
    	const char* jsonStr = R"({"FxData":[{"path":"abc1", "age":12},{"path":"abc2", "age":12},{"path":"abc3", "age":12}], "Audio":"click"})";
    	const auto& J = json::parse(jsonStr, nullptr, false);
    	if (J.contains("Audio"))
    	{
    		JsonData* jsonData = new JsonData();
    		const basic_json<>& list = J["FxData"];
    		bool isArray = list.is_array();
    		if (isArray)
    		{
    			for (size_t i = 0; i < list.size(); i++)
    			{
    				const auto& item = list[i];
    				item.get_to(*jsonData); //这里就是因为定义了from_json,才可以使用get_to的方法。
    				string path = "ddd"; //默认值
    				string path2;
    				int age;
    				path2 = item.value("path3", path); //因为没有key=path3的,所以使用path填充path2
    				item.at("path").get_to(path);
    			}
    		}
    	}
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    使用宏展开的方式:

    NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(JsonData,
    	path,
    	age)
    
    • 1
    • 2
    • 3

    其中的宏定义在json.hpp中:

    #define NLOHMANN_JSON_FROM_WITH_DEFAULT(v1) nlohmann_json_t.v1 = nlohmann_json_j.value(#v1, nlohmann_json_t.v1);
    /*!
    @brief macro
    @def NLOHMANN_DEFINE_TYPE_INTRUSIVE
    @since version 3.9.0
    */
    #define NLOHMANN_DEFINE_TYPE_INTRUSIVE(Type, ...)  \
        friend void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) }
    
    #define NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(Type, ...)  \
        friend void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { Type nlohmann_json_default_obj; NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) }
    
    /*!
    @brief macro
    @def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE
    @since version 3.9.0
    */
    #define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Type, ...)  \
        inline void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) }
    
    #define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(Type, ...)  \
        inline void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { /*Type nlohmann_json_default_obj;*/ NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    nlohmann_json_j.value(#v1, nlohmann_json_t.v1);这里使用json.value的取值方式。

  • 相关阅读:
    [JavaEE系列] 多线程常见的锁策略及synchronized底层工作过程
    从softmax分类问题看神经网络的核心
    使用云API管理你的云服务器
    《对比Excel,轻松学习Python数据分析》读书笔记------数据分组与数据透视表
    JavaWeb-WEB请求过程
    打卡信奥刷题(90)用Scratch图形化工具信奥P1853 [普及组] 投资的最大效益
    webrtc终极版(二)搭建自己的iceserver服务,并用到RTCMultiConnection的demo中
    股票交易接口的交易量是什么
    【GlobalMapper精品教程】013:矢量点图层的创建及数字化案例操作
    JAVA最全面试题汇总基础篇(一)
  • 原文地址:https://blog.csdn.net/wodownload2/article/details/126000340