C++構(gòu)造和解析Json的使用示例
概述
JSON是一種輕量級(jí)的數(shù)據(jù)交互格式,易于人閱讀和編寫,同時(shí)也易于機(jī)器解析和生成,并有效地提升網(wǎng)絡(luò)傳輸效率,實(shí)際項(xiàng)目中經(jīng)常用到,相比xml有很多優(yōu)點(diǎn),問問度娘,優(yōu)點(diǎn)一籮筐。
第三方庫
json解析選用jsoncpp作為第三方庫,jsoncpp使用廣泛,c++開發(fā)首選。
jsoncpp目前已經(jīng)托管到了github上,地址:https://github.com/open-source-parsers/jsoncpp
使用
使用c++進(jìn)行構(gòu)造json和解析json,選用vs2010作為IDE。工程中使用jsoncpp的源碼進(jìn)行編譯,沒有使用jsoncpp的庫,為方便大家使用把dll和lib庫也放到了我的工程jsoncpplib文件夾下,有需要的可以直接引用庫。
待解析的json數(shù)據(jù)格式如下圖:
/******************************************************** 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; }
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)我們的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
上一篇:C++11并發(fā)編程關(guān)于原子操作atomic的代碼示例
欄 目:C語言
下一篇:C++標(biāo)準(zhǔn)模板庫map的常用操作
本文標(biāo)題:C++構(gòu)造和解析Json的使用示例
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/576.html
您可能感興趣的文章
- 04-02c語言沒有round函數(shù) round c語言
- 01-10求子數(shù)組最大和的解決方法詳解
- 01-10深入理解C++中常見的關(guān)鍵字含義
- 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
- 01-10c++中inline的用法分析
- 01-10用C++實(shí)現(xiàn)DBSCAN聚類算法
- 01-10全排列算法的非遞歸實(shí)現(xiàn)與遞歸實(shí)現(xiàn)的方法(C++)
- 01-10C++大數(shù)模板(推薦)
- 01-10淺談C/C++中的static與extern關(guān)鍵字的使用詳解
- 01-10深入C/C++浮點(diǎn)數(shù)在內(nèi)存中的存儲(chǔ)方式詳解


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對(duì)數(shù)函數(shù)的表達(dá)式 c語言中對(duì)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 01-10delphi制作wav文件的方法
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 04-02jquery與jsp,用jquery
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置