C++構(gòu)造和解析Json的使用示例
概述
JSON是一種輕量級(jí)的數(shù)據(jù)交互格式,易于人閱讀和編寫(xiě),同時(shí)也易于機(jī)器解析和生成,并有效地提升網(wǎng)絡(luò)傳輸效率,實(shí)際項(xiàng)目中經(jīng)常用到,相比xml有很多優(yōu)點(diǎn),問(wèn)問(wèn)度娘,優(yōu)點(diǎn)一籮筐。
第三方庫(kù)
json解析選用jsoncpp作為第三方庫(kù),jsoncpp使用廣泛,c++開(kāi)發(fā)首選。
jsoncpp目前已經(jīng)托管到了github上,地址:https://github.com/open-source-parsers/jsoncpp
使用
使用c++進(jìn)行構(gòu)造json和解析json,選用vs2010作為IDE。工程中使用jsoncpp的源碼進(jìn)行編譯,沒(méi)有使用jsoncpp的庫(kù),為方便大家使用把dll和lib庫(kù)也放到了我的工程jsoncpplib文件夾下,有需要的可以直接引用庫(kù)。
待解析的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é)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
如何在c++中實(shí)現(xiàn)字符串分割函數(shù)split詳解
這篇文章主要給大家介紹了關(guān)于如何在c++中實(shí)現(xiàn)字符串分割函數(shù)split的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用c++具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11C語(yǔ)言使用mciSendString實(shí)現(xiàn)播放音樂(lè)功能
mciSendString?支持?mp3、wma、wav、mid?等多種媒體格式,使用非常簡(jiǎn)單。這篇文章就來(lái)為大家介紹一下C語(yǔ)言如何使用mciSendString實(shí)現(xiàn)播放音樂(lè)功能,需要的可以參考一下2023-02-02C語(yǔ)言實(shí)現(xiàn)打印星號(hào)圖案
這篇文章主要介紹了C語(yǔ)言實(shí)現(xiàn)打印星號(hào)圖案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11C語(yǔ)言數(shù)組按協(xié)議存儲(chǔ)與按協(xié)議解析數(shù)據(jù)的實(shí)現(xiàn)
今天小編就為大家分享一篇關(guān)于C語(yǔ)言數(shù)組按協(xié)議存儲(chǔ)與按協(xié)議解析數(shù)據(jù)的實(shí)現(xiàn),小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12C語(yǔ)言中動(dòng)態(tài)內(nèi)存分配malloc、calloc和realloc函數(shù)解析
C語(yǔ)言跟內(nèi)存申請(qǐng)相關(guān)的函數(shù)主要有 alloca、calloc、malloc、free、realloc等,下面這篇文章主要給大家介紹了關(guān)于C語(yǔ)言中動(dòng)態(tài)內(nèi)存分配malloc、calloc和realloc函數(shù)的相關(guān)資料,需要的朋友可以參考下2022-03-03