• c++ CJsonObject 读写json


    一、工具名称

    CJsonObject

    二、下载安装渠道

    CJsonObject 通过CSDN官方开发的【猿如意】客户端进行下载安装。

    2.1 什么是猿如意?

    猿如意是一款面向开发者的辅助开发工具箱,包含了效率工具、开发工具下载,教程文档,代码片段搜索,全网搜索等功能模块。帮助开发者提升开发效率,帮你从“问题”找到“答案”。

    2.2 如何下载猿如意?

    点击链接,登录猿如意官网即可下载https://devbit.csdn.net?source=csdn_community

    2.3 如何在载猿如意中下载开发工具?

    【猿如意】安装完成后,在顶部搜搜框输入链接c++ CJsonObject 读写json_子建莫敌的博客-CSDN博客_cjsonobject,选择下方“全网搜索”然后点击打开文章,获取文章里面的链接“ GitHub地址:https://github.com/Bwar/CJsonObject”即可下载对应的开发工具,之后按步骤即可完成安装。

    三、CJsonObject介绍

            CJsonObject是Bwar基于cJSON全新开发一个C++版的JSON库,CJsonObject的最大优势是简单、轻量、跨平台,开发效率极高,尤其对多层嵌套json的读取和生成、修改极为方便。CJsonObject比cJSON简单易用得多,且只要不是有意不释放内存就不会发生内存泄漏。用CJsonObject的好处在于完全不用专门的文档,头文件即文档,所有函数都十分通俗易懂,最为关键的一点是解析JSON和生成JSON的编码效率非常高。
    GitHub地址:https://github.com/Bwar/CJsonObject

    四、CJsonObject使用

            简单到只需往项目添加四个文件:cJson.h、cJson.c、CJsonObject.hpp、CJsonObject.cpp。

    在使用时只需包含一个头文件

    #include "CJsonObject.hpp"

    vs2019编译提示“ error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.”

    解决办法:在vs项目属性 ——C/C++——预处理——预处理器定义中新增,添加“_CRT_SECURE_NO_WARNINGS”,如下图所示:

     CJsonObject示例:

    • 写json文件:

    1. #include
    2. void writeJsonFile(const std::string fileName)
    3. {
    4. std::ofstream os(fileName);
    5. if (!os) return;
    6. neb::CJsonObject root;
    7. root.Add("age", 21);
    8. neb::CJsonObject friendsObj;
    9. friendsObj.Add("firend_age", 21);
    10. friendsObj.Add("firend_name", "ZhouWuxian");
    11. friendsObj.Add("firend_sex", "man");
    12. root.Add("friends", friendsObj);
    13. neb::CJsonObject hobbyObj;
    14. hobbyObj.Add("sing");
    15. hobbyObj.Add("run");
    16. hobbyObj.Add("Tai Chi");
    17. root.Add("hobby", hobbyObj);
    18. root.Add("name", "shuiyixin");
    19. root.Add("sex", "man");
    20. //os << root.ToString(); // 格式化输出
    21. os << root.ToFormattedString(); // 非格式化输出
    22. os.close();
    23. return;
    24. }

    json文件内容:

    • 读json文件

    1. #include
    2. void readJsonFile(const std::string fileName)
    3. {
    4. std::ifstream file;
    5. file.open(fileName, std::ios::in);
    6. //指针定位到文件末尾
    7. file.seekg(0, std::ios::end);
    8. int fileLength = file.tellg();
    9. //指定定位到文件开始
    10. file.seekg(0, std::ios::beg);
    11. char* buffer = new char[fileLength + 1];
    12. file.read(buffer, fileLength);
    13. buffer[fileLength] = '\0';
    14. std::string strJson = buffer;
    15. if (buffer) {
    16. delete[] buffer;
    17. }
    18. file.close();
    19. neb::CJsonObject root;
    20. if (!root.Parse(strJson)) return;
    21. int age = 0;
    22. root.Get("age", age);
    23. root.Add("age", 21);
    24. neb::CJsonObject friendsObj;
    25. root.Get("friends", friendsObj);
    26. if (friendsObj.IsEmpty()) return;
    27. int friend_age = 0;
    28. friendsObj.Get("firend_age", friend_age);
    29. std::string friend_name;
    30. friendsObj.Get("firend_name", friend_name);
    31. std::string friend_sex;
    32. friendsObj.Get("firend_sex", friend_sex);
    33. neb::CJsonObject hobbyObj;
    34. root.Get("hobby", hobbyObj);
    35. if (hobbyObj.IsEmpty() && hobbyObj.IsArray()) return;
    36. for (int i = 0; i < hobbyObj.GetArraySize(); ++i) {
    37. std::string info;
    38. hobbyObj.Get(i, info);
    39. std::cout << info << std::endl;
    40. }
    41. std::string name;
    42. root.Get("name", name);
    43. std::string sex;
    44. root.Get("sex", sex);
    45. return;
    46. }
  • 相关阅读:
    论文《Unsupervised Dialog Structure Learning》笔记:详解DD-VRNN
    MySQL数据库(一)
    Istio 自动注入 sidecar 失败导致无法访问webhook服务
    详解typora配置华为云图床
    Linux一篇入门(以Ubuntu为例)
    Java中介者模式
    uniapp项目实践总结(十九)版本更新和热更新实现方法
    振南技术干货集:深入浅出的Bootloader(4)
    vr火灾逃生安全科普软件开展消防突击教育安全有效
    题解:ABC321F - #(subset sum = K) with Add and Erase
  • 原文地址:https://blog.csdn.net/u013015629/article/details/128079895