博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C/C++由字符串转JSON/JSON转字符串/数组解析/数组添加
阅读量:4302 次
发布时间:2019-05-27

本文共 2031 字,大约阅读时间需要 6 分钟。

字符串转成JSON(其中str为字符串)

1

2

3

4

5

Json::Reader Reader;

Json::Value DevJson;

Reader.parse(str,DevJson);

int dev_id = DevJson["dev_id"].asInt();

int index = DevJson["index"].asInt();

  

JSON转字符串(其中DevStr为字符串)

1

2

3

Json::Value DevJson = DevsJson[i];

std::string DevStr = DevJson.toStyledString();

printf("Msg:%s", DevStr.c_str());

  

JSON数组解析:

1

2

3

4

5

6

7

8

9

10

Json::Reader Reader;

 Json::Value DevsJson;

 Reader.parse(MsgStr, DevsJson);

 int siNum = DevsJson.size();

 for(int i=0; i < siNum; i++)

 {

 Json::Value DevJson = DevsJson[i];

 std::string DevStr = DevJson.toStyledString();

 printf("Msg:%s", DevStr.c_str());

 }

  

数组添加:

1

2

3

4

5

Json::Value root;

Json::Value person;

person["name"] = "hello world";

person["age"] = 100;

root.append(person);

  

结果:[{"age":100,"name":"hello world"}]

---------------------------------------------------

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

54

55

56

// MyTest.cpp : 定义控制台应用程序的入口点。

//

#include "stdafx.h"

#include "iostream"

#include "time.h"

#include "map"

#include <windows.h>

#include <sstream>

#include <list>

#include "json\json.h" 

#include "stdint.h"

using namespace std;

 

int _tmain(int argc, _TCHAR* argv[])

{

    Json::Value value;

    uint32_t ssrc1 = 5305490;

    value["ssrc1"] = ssrc1;

 

    uint32_t ssrc2 = 2152748638;

    value["ssrc2"] = ssrc2;

     

    std::string body = value.toStyledString();

    cout << body << endl;

    Json::Reader reader;

    Json::Value data;

    reader.parse(body, data, false);

 

    if (data.isMember("ssrc1"))

    {

        cout << "ssrc1";

        if (data["ssrc1"].isInt())

            cout << " is Int" << endl;

 

        if (data["ssrc1"].isUInt())

            cout << " is UInt" << endl;

    }

    if (data.isMember("ssrc2"))

    {

        cout << "ssrc2";

        if (data["ssrc2"].isInt())

            cout << " is Int" << endl;

 

        if (data["ssrc2"].isUInt())

            cout << " is UInt" << endl;

    }

 

    uint32_t ssrc11 = data["ssrc1"].asUInt();

    uint32_t ssrc21 = data["ssrc2"].asUInt();

 

    cout << "ssrc11:" << ssrc11 << endl;

    cout << "ssrc21:" << ssrc21 << endl;

 

    system("pause");

    return 0;

}

输出结果:

转载地址:http://ewlws.baihongyu.com/

你可能感兴趣的文章
suse如何修改ssh端口为2222?
查看>>
详细理解“>/dev/null 2>&1”
查看>>
suse如何创建定时任务?
查看>>
suse搭建ftp服务器方法
查看>>
centos虚拟机设置共享文件夹并通过我的电脑访问[增加smbd端口修改]
查看>>
文件拷贝(IFileOperation::CopyItem)
查看>>
MapReduce的 Speculative Execution机制
查看>>
大数据学习之路------借助HDP SANDBOX开始学习
查看>>
Hadoop基础学习:基于Hortonworks HDP
查看>>
为什么linux安装程序 都要放到/usr/local目录下
查看>>
Hive安装前扫盲之Derby和Metastore
查看>>
永久修改PATH环境变量的几种办法
查看>>
大数据学习之HDP SANDBOX开始学习
查看>>
Hive Beeline使用
查看>>
Centos6安装图形界面(hdp不需要,hdp直接从github上下载数据即可)
查看>>
CentOS7 中把yum源更换成163源
查看>>
关于yum Error: Cannot retrieve repository metadata (repomd.xml) for repository:xxxxxx.
查看>>
linux下载github中的文件
查看>>
HDP Sandbox里面git clone不了数据(HTTP request failed)【目前还没解决,所以hive的练习先暂时搁置了】
查看>>
动态分区最佳实践(一定要注意实践场景)
查看>>