当前位置: 移动技术网 > IT编程>开发语言>C/C++ > C++构造和解析JSON

C++构造和解析JSON

2019年10月17日  | 移动技术网IT编程  | 我要评论

重活传说,热情小娇妻,者尼私人影院

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数据格式如下图:

file

/********************************************************
copyright (c), 2016-2017,
filename:   main
author:     woniu201
email:      wangpengfei.201@163.com
created:    2017/09/06
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 
@ created:      2017/09/06
@ 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 
@ created:      2017/09/06
@ 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 
@ created:      2017/09/06
@ 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 
@ created:      2017/09/06
@ 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;
}

关注下面公众号,回复"104"获取源码
file

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网