概述
JSON是一種輕量級的數據交互格式,易于人閱讀和編寫,同時也易于機器解析和生成,并有效地提升網絡傳輸效率,實際項目中經常用到,相比xml有很多優點,問問度娘,優點一籮筐。
第三方庫
json解析選用jsoncpp作為第三方庫,jsoncpp使用廣泛,c++開發首選。
jsoncpp目前已經托管到了github上,地址:https://github.com/open-source-parsers/jsoncpp
使用
使用c++進行構造json和解析json,選用vs2010作為IDE。工程中使用jsoncpp的源碼進行編譯,沒有使用jsoncpp的庫,為方便大家使用把dll和lib庫也放到了我的工程jsoncpplib文件夾下,有需要的可以直接引用庫。
待解析的json數據格式如下圖:
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
/******************************************************** Copyright (C), 2016-2017, FileName: main Author: woniu201 Description:use jsoncpp src , not use dll, but i also provide dll and lib. ********************************************************/ #include "stdio.h" #include <string> #include "jsoncpp/json.h" using namespace std; /************************************ @ Brief: read file @ Author: woniu201 @ Return: file data ************************************/ char *getfileAll( char *fname) { FILE *fp; char *str; char txt[1000]; int filesize; if ((fp= fopen (fname, "r" ))==NULL){ printf ( "open file %s fail \n" ,fname); return NULL; } fseek (fp,0,SEEK_END); filesize = ftell (fp); str=( char *) malloc (filesize); str[0]=0; rewind (fp); while (( fgets (txt,1000,fp))!=NULL){ strcat (str,txt); } fclose (fp); return str; } /************************************ @ Brief: write file @ Author: woniu201 @ Return: ************************************/ int writefileAll( char * fname, const char * data) { FILE *fp; if ((fp= fopen (fname, "w" )) == NULL) { printf ( "open file %s fail \n" , fname); return 1; } fprintf (fp, "%s" , data); fclose (fp); return 0; } /************************************ @ Brief: parse json data @ Author: woniu201 @ Return: ************************************/ int parseJSON( const char * jsonstr) { Json::Reader reader; Json::Value resp; if (!reader.parse(jsonstr, resp, false )) { printf ( "bad json format!\n" ); return 1; } int result = resp[ "Result" ].asInt(); string resultMessage = resp[ "ResultMessage" ].asString(); printf ( "Result=%d; ResultMessage=%s\n" , result, resultMessage.c_str()); Json::Value & resultValue = resp[ "ResultValue" ]; for ( int i=0; i<resultValue.size(); i++) { Json::Value subJson = resultValue[i]; string cpuRatio = subJson[ "cpuRatio" ].asString(); string serverIp = subJson[ "serverIp" ].asString(); string conNum = subJson[ "conNum" ].asString(); string websocketPort = subJson[ "websocketPort" ].asString(); string mqttPort = subJson[ "mqttPort" ].asString(); string ts = subJson[ "TS" ].asString(); printf ( "cpuRatio=%s; serverIp=%s; conNum=%s; websocketPort=%s; mqttPort=%s; ts=%s\n" ,cpuRatio.c_str(), serverIp.c_str(), conNum.c_str(), websocketPort.c_str(), mqttPort.c_str(), ts.c_str()); } return 0; } /************************************ @ Brief: create json data @ Author: woniu201 @ Return: ************************************/ int createJSON() { Json::Value req; req[ "Result" ] = 1; req[ "ResultMessage" ] = "200" ; Json::Value object1; object1[ "cpuRatio" ] = "4.04" ; object1[ "serverIp" ] = "42.159.116.104" ; object1[ "conNum" ] = "1" ; object1[ "websocketPort" ] = "0" ; object1[ "mqttPort" ] = "8883" ; object1[ "TS" ] = "1504665880572" ; Json::Value object2; object2[ "cpuRatio" ] = "2.04" ; object2[ "serverIp" ] = "42.159.122.251" ; object2[ "conNum" ] = "2" ; object2[ "websocketPort" ] = "0" ; object2[ "mqttPort" ] = "8883" ; object2[ "TS" ] = "1504665896981" ; Json::Value jarray; jarray.append(object1); jarray.append(object2); req[ "ResultValue" ] = jarray; Json::FastWriter writer; string jsonstr = writer.write(req); printf ( "%s\n" , jsonstr.c_str()); writefileAll( "createJson.json" , jsonstr.c_str()); return 0; } int main() { char * json = getfileAll( "parseJson.json" ); parseJSON(json); printf ( "===============================\n" ); createJSON(); getchar (); return 1; } |
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。如果你想了解更多相關內容請查看下面相關鏈接
原文鏈接:https://blog.csdn.net/woniu211111/article/details/77866983