日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C/C++由字符串转JSON/JSON转字符串/数组解析/数组添加

發布時間:2023/12/18 c/c++ 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C/C++由字符串转JSON/JSON转字符串/数组解析/数组添加 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

字符串轉成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;

}

輸出結果:

總結

以上是生活随笔為你收集整理的C/C++由字符串转JSON/JSON转字符串/数组解析/数组添加的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。