C++構(gòu)造和解析Json的使用示例
概述
JSON是一種輕量級的數(shù)據(jù)交互格式,易于人閱讀和編寫,同時也易于機(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)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
相關(guān)文章
C語言中find_package()的搜索路徑的實(shí)現(xiàn)
本文主要介紹了C語言中find_package()的搜索路徑的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12
基于Matlab實(shí)現(xiàn)離散系統(tǒng)分岔圖的繪制
這篇文章主要介紹了如何利用Matlab實(shí)現(xiàn)離散分岔圖的繪制,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Matlab有一定的幫助,需要的可以參考一下2022-04-04
Qt數(shù)據(jù)庫應(yīng)用之實(shí)現(xiàn)通用數(shù)據(jù)庫請求
這篇文章主要為大家介紹了Qt中是如何實(shí)現(xiàn)通用數(shù)據(jù)庫請求的,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Qt有一定幫助,感興趣的小伙伴可以了解一下2022-03-03
C語言函數(shù)指針數(shù)組實(shí)現(xiàn)計(jì)算器功能
這篇文章主要通過C語言函數(shù)指針數(shù)組實(shí)現(xiàn)了計(jì)算器的功能,是一個很好而且流程詳細(xì)的小例子,感興趣的新手朋友們可以自己動手也寫一遍2022-04-04
C語言SetConsoleCursorPosition函數(shù)使用方法
這篇文章介紹了C語言SetConsoleCursorPosition函數(shù)的使用方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12
C++項(xiàng)目開發(fā)實(shí)現(xiàn)圖書管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C++項(xiàng)目開發(fā)實(shí)現(xiàn)圖書管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03
C語言內(nèi)存的動態(tài)分配比較malloc和realloc的區(qū)別
這篇文章主要介紹了C語言內(nèi)存的動態(tài)分配比較malloc和realloc的區(qū)別,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是本文的詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C語言?模擬實(shí)現(xiàn)strlen函數(shù)詳解
在 C 語言 中我們要獲取 字符串 的長度,可以使用strlen 函數(shù),strlen 函數(shù)計(jì)算字符串的長度時,直到空結(jié)束字符,但不包括空結(jié)束字符,因?yàn)?nbsp;strlen 函數(shù)時不包含最后的結(jié)束字符的,因此一般使用 strlen函數(shù)計(jì)算的字符串的長度會比使用 sizeof 計(jì)算的字符串的字節(jié)數(shù)要小2022-04-04

