IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C++ JSON处理库 CJsonObject 的使用 -> 正文阅读

[C++知识库]C++ JSON处理库 CJsonObject 的使用

一、简介

CJsonObject 是一个基于cJSON开发的、适用于c++的轻量级的JSON处理器。
开源地址:
https://gitee.com/Bwar/CJsonObject.git

二、使用示例

1. 解析一个json格式文件

2.1.1 a.json

{"a":"hello"}

2.1.2 解析程序

#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include "../CJsonObject.hpp"

using namespace std;
int main(int argc, char* argv[])
{
    std::ifstream fin(argv[1]);
    std::string strValue;
    if (fin.good())
    {
        neb::CJsonObject oJson;
        std::stringstream ssContent;
        ssContent << fin.rdbuf();
        if (oJson.Parse(ssContent.str()))
        {
            std::cout << oJson.ToString() << std::endl;
            oJson.Get("a", strValue);
	        std::cout << "a=" << strValue << std::endl;
        }
        else
        {
            std::cerr << "parse json error" << "\n";// << ssContent.str() << std::endl;
        }
        fin.close();
    }
}

2.1.3 运行结果

./CJsonObjectTest "a.js" 
{"a":"12345"}
a=12345

2. 解析一个字符串并读取键值

2.2.1 代码

#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include "../CJsonObject.hpp"

using namespace std;

int main(int argc, char* argv[])
{
    std::string strValue;
   
    int iValue;
    double fTimeout;
    
    neb::CJsonObject oJson("{\"refresh_interval\":60,"
                        "\"test_float\":[18.0, 10.0, 5.0],"
                        "\"test_int\":[135355, -1844674407370955161, -935375],"
                        "\"timeout\":12.5,"
                        "\"dynamic_loading\":["
                            "{"
                                "\"so_path\":\"plugins/User.so\", \"load\":false, \"version\":1,"
                                "\"cmd\":["
                                     "{\"cmd\":2001, \"class\":\"neb::CmdUserLogin\"},"
                                     "{\"cmd\":2003, \"class\":\"neb::CmdUserLogout\"}"
                                "],"
                                "\"module\":["
                                     "{\"path\":\"im/user/login\", \"class\":\"neb::ModuleLogin\"},"
                                     "{\"path\":\"im/user/logout\", \"class\":\"neb::ModuleLogout\"}"
                                "]"
                             "},"
                             "{"
                             "\"so_path\":\"plugins/ChatMsg.so\", \"load\":false, \"version\":1,"
                                 "\"cmd\":["
                                      "{\"cmd\":2001, \"class\":\"neb::CmdChat\"}"
                                 "],"
                             "\"module\":[]"
                             "}"
                        "]"
                    "}");
     cout << oJson.ToString() << endl;
     cout << "-------------------------------------------------------------------" << endl;
     cout << oJson["dynamic_loading"][0]["cmd"][1]("class") << endl;
}

2.2.2 输出

./CJsonObjectTest 
{"refresh_interval":60,"test_float":[18.000000000000000,10.000000000000000,5.000000000000000],"test_int":[135355,-1844674407370955161,-935375],"timeout":12.500000000000000,"dynamic_loading":[{"so_path":"plugins/User.so","load":false,"version":1,"cmd":[{"cmd":2001,"class":"neb::CmdUserLogin"},{"cmd":2003,"class":"neb::CmdUserLogout"}],"module":[{"path":"im/user/login","class":"neb::ModuleLogin"},{"path":"im/user/logout","class":"neb::ModuleLogout"}]},{"so_path":"plugins/ChatMsg.so","load":false,"version":1,"cmd":[{"cmd":2001,"class":"neb::CmdChat"}],"module":[]}]}
-------------------------------------------------------------------
neb::CmdUserLogout

3. 取值及修改值

    int iValue;
    double fTimeout;

    oJson["dynamic_loading"][0]["cmd"][0].Get("cmd", iValue);
    cout << "iValue = " << iValue << endl;
    oJson["dynamic_loading"][0]["cmd"][0].Replace("cmd", -2001);
    oJson["dynamic_loading"][0]["cmd"][0].Get("cmd", iValue);
    cout << "iValue = " << iValue << endl;

    oJson.Get("timeout", fTimeout);

输出:

./CJsonObjectTest 
iValue = 2001
iValue = -2001

4. 增加键

2.4.1 代码

    neb::CJsonObject oJson("{\"refresh_interval\":60,"
                        "\"test_float\":[18.0, 10.0, 5.0],"
                        "\"timeout\":12.5"
                    "}");


     oJson.AddEmptySubObject("depend");
     oJson["depend"].Add("nebula", "https://github.com/Bwar/Nebula");
     oJson["depend"].AddEmptySubArray("bootstrap");
     oJson["depend"]["bootstrap"].Add("BEACON");
     oJson["depend"]["bootstrap"].Add("LOGIC");
     oJson["depend"]["bootstrap"].Add("LOGGER");
     oJson["depend"]["bootstrap"].Add("INTERFACE");
     oJson["depend"]["bootstrap"].Add("ACCESS");
     cout << oJson.ToFormattedString() << endl;

2.4.2 输出

./CJsonObjectTest 
{
        "refresh_interval":     60,
        "test_float":   [18.000000000000000, 10.000000000000000, 5.000000000000000],
        "timeout":      12.500000000000000,
        "depend":       {
                "nebula":       "https://github.com/Bwar/Nebula",
                "bootstrap":    ["BEACON", "LOGIC", "LOGGER", "INTERFACE", "ACCESS"]
        }
}

5. 删除键

oCopyJson["depend"]["bootstrap"].Delete(1);

6. 复制对象

    neb::CJsonObject oCopyJson = oJson;
     if (oCopyJson == oJson)
     {
         cout << "json equal" << endl;
     }

输出 :

json equal

7. 遍历键值

2.7.1 代码

    neb::CJsonObject oJson("{\"refresh_interval\":60,"
                        "\"test_float\":[18.0, 10.0, 5.0],"
                        "\"test_int\":[135355, -1844674407370955161, -935375],"
                        "\"timeout\":12.5,"
                        "\"dynamic_loading\":["
                            "{"
                                "\"so_path\":\"plugins/User.so\", \"load\":false, \"version\":1,"
                                "\"cmd\":["
                                     "{\"cmd\":2001, \"class\":\"neb::CmdUserLogin\"},"
                                     "{\"cmd\":2003, \"class\":\"neb::CmdUserLogout\"}"
                                "],"
                                "\"module\":["
                                     "{\"path\":\"im/user/login\", \"class\":\"neb::ModuleLogin\"},"
                                     "{\"path\":\"im/user/logout\", \"class\":\"neb::ModuleLogout\"}"
                                "]"
                             "},"
                             "{"
                             "\"so_path\":\"plugins/ChatMsg.so\", \"load\":false, \"version\":1,"
                                 "\"cmd\":["
                                      "{\"cmd\":2001, \"class\":\"neb::CmdChat\"}"
                                 "],"
                             "\"module\":[]"
                             "}"
                        "]"
                    "}");


     string strTraversing;
     while(oJson["dynamic_loading"][0].GetKey(strTraversing))
     {
        cout << "traversing:  " << strTraversing << endl;
     }

2.7.2 输出

./CJsonObjectTest 
traversing:  so_path
traversing:  load
traversing:  version
traversing:  cmd
traversing:  module

8. 增加键

     oJson["dynamic_loading"][0].Add("new_key", "new_value");
     while(oJson["dynamic_loading"][0].GetKey(strTraversing))
     {
         cout << "traversing:  " << strTraversing << endl;
     }

9. Get方法数组值

2.9.1 代码

    neb::CJsonObject oJson("{\"refresh_interval\":60,"
                        "\"test_float\":[18.0, 10.0, 5.0],"
                        "\"test_int\":[135355, -1844674407370955161, -935375],"
                        "\"timeout\":12.5"
                    "}");


     cout << oJson["test_float"].GetArraySize() << endl;
     float fTestValue = 0.0;
     for (int i = 0; i < oJson["test_float"].GetArraySize(); ++i)
     {
         oJson["test_float"].Get(i, fTestValue);
         cout << fTestValue << "\t in string: " << oJson["test_float"](i) << endl;
     }

2.9.2 输出

./CJsonObjectTest 
3
18       in string: 18.000000
10       in string: 10.000000
5        in string: 5.000000

10. 函数下标取数组值

2.10.1 代码

    neb::CJsonObject oJson("{\"refresh_interval\":60,"
                        "\"test_float\":[18.0, 10.0, 5.0],"
                        "\"test_int\":[135355, -1844674407370955161, -935375],"
                        "\"timeout\":12.5"
                    "}");



     for (int i = 0; i < oJson["test_int"].GetArraySize(); ++i)
     {
         cout << "in string: " << oJson["test_int"](i) << endl;
     }

2.10.2 输出

./CJsonObjectTest 
in string: 135355
in string: -1844674407370955161
in string: -935375

11. 空值作为键和值

2.11.1 代码

    neb::CJsonObject oJson("{\"refresh_interval\":60,"
                        "\"test_float\":[18.0, 10.0, 5.0],"
                        "\"test_int\":[135355, -1844674407370955161, -935375],"
                        "\"timeout\":12.5"
                    "}");



     oJson.AddNull("null_value");
     cout << oJson.IsNull("test_float") << "\t" << oJson.IsNull("null_value") << endl;
     oJson["test_float"].AddNull();
     cout << oJson.ToString() << endl;

2.11.2 输出

./CJsonObjectTest 
0       1
{"refresh_interval":60,"test_float":[18.000000000000000,10.000000000000000,5.000000000000000,null],"test_int":[135355,-1844674407370955161,-935375],"timeout":12.500000000000000,"null_value":null}

12. 判断键是否存在

     if (oJson.KeyExist("timeout"))
         cout << "timeout key exist" << endl;
  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-03-06 12:43:52  更:2022-03-06 12:44:13 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 5:56:58-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码