jsoncpp文件解析和相关例程

=================================================================================
                                Jsoncpp 使用与移植
=================================================================================
JSONCPP 源码下载地址:    http://sourceforge.net/projects/jsoncpp/
jsoncpp目前已经托管到了github上,地址:https://github.com/open-source-parsers/jsoncpp

1. linux 下的编译 与使用
    1.1 先安装 scons
        sudo apt-get install scons
        scons platform=linux-gcc

2. arm平台编译    注:platform 没有包含 arm 平台,类似 linux-gcc,所以把源码提取出来,独立编译
    $ mkdir arm_jsoncpp
    $ cp include/ arm_jsoncpp/ -r
    $ cp src/lib_json/* arm_jsoncpp/
    $ cd arm_jsoncpp/
     
    # 编译静态库
    $ arm-linux-gnueabihf-g++ -c *.cpp -I./include -fPIC
    $ ar cr libjsoncpp.a *.o
     
    # 编译动态库
    $ arm-linux-gnueabihf-g++ -shared -fPIC *.cpp -I./include -o libjsoncpp.so
    ————————————————

 代码示例: example.c


/********************************************************
Copyright (C), 2019-2020,
FileName: 	jsoncpp_api
Author: 	yangxuejian
Email: 		[email protected]
Created: 	2019.09.27
********************************************************/

extern "C"
{
#include 
#include 
#include 
}

#include 

using namespace std;

#include 
#include  

unsigned char* Jsoncpp_ReadContent_FromFile(char *ps8FileName)
{
    int iRet = 0;
    int iSize = 0;
    T_FileOpr tFileOpr = {0};
    unsigned char *pu8ContentBuf = NULL;

    iRet = File_Open(&tFileOpr, ps8FileName, (char*)"r+");
    if(iRet < 0)
    {
        cout << ps8FileName << "File_Open Fail !" << endl;
        goto JSONCPP_GETCONTENT_FROMFILE_ERROR;
    }

    iRet = File_Map(&tFileOpr);
    if(iRet < 0)
    {
        printf("File_Map Fail !\n");
        goto JSONCPP_GETCONTENT_FROMFILE_ERROR1;
    }

    pu8ContentBuf = new unsigned char[tFileOpr.iFileSize];
    if(pu8ContentBuf == NULL)
    {
        cout << "New Memary Error for File: " << ps8FileName << endl;
        goto JSONCPP_GETCONTENT_FROMFILE_ERROR2;
    }

    iSize = File_ReadExt(&tFileOpr, tFileOpr.iFileSize, 0, pu8ContentBuf);
    if(iSize <=0)
    {
        printf("File_ReadExt Fail!\n");
        goto JSONCPP_GETCONTENT_FROMFILE_ERROR3;
    }

    goto JSONCPP_GETCONTENT_FROMFILE_ERROR2;
    
JSONCPP_GETCONTENT_FROMFILE_ERROR3:
    delete [] pu8ContentBuf;
    pu8ContentBuf = NULL;
JSONCPP_GETCONTENT_FROMFILE_ERROR2:
    File_UnMap(&tFileOpr);
JSONCPP_GETCONTENT_FROMFILE_ERROR1:
    File_Close(&tFileOpr);
JSONCPP_GETCONTENT_FROMFILE_ERROR:
    return pu8ContentBuf;

}

int Jsoncpp_WriteContent_ToFile(char* ps8FileName, const unsigned char* pu8ContentBuf, int s32Size)
{
    int iRet = 0;
    T_FileOpr tFileOpr = {0};

    iRet = File_Open(&tFileOpr, ps8FileName, (char*)"w+");
    if(iRet < 0)
    {
        cout << ps8FileName << "File_Open Fail !" << endl;
        return -1;
    }

    iRet = File_Write(&tFileOpr, s32Size, (const unsigned char *)pu8ContentBuf);
    if(iRet < 0)
    {
        cout << ps8FileName << "File_Write Fail !" << endl;
        return -1;
    }
	
	return iRet;
}
 
int Jsoncpp_ParseContent_Example(PT_Jsoncpp_Example pstJsoncppExamp, const unsigned char* pu8ContentBuf)
{
	Json::Reader Reader;
	Json::Value  Resp;
    
 
	if (!Reader.parse((const char* )pu8ContentBuf, Resp, false))
	{
		cout << "bad json format!\n" << endl;
		return 1;
	}
    
	pstJsoncppExamp->s32Rst  = Resp["Result"].asInt();
	pstJsoncppExamp->sRstMsg = Resp["ResultMessage"].asString();
    
	printf("Result=%d; ResultMessage=%s\n", pstJsoncppExamp->s32Rst, pstJsoncppExamp->sRstMsg.c_str());
 
	Json::Value & JRstValue = Resp["ResultValue"];
	for (unsigned int i = 0; i < JRstValue.size(); i++)
	{
		Json::Value JSubJson = JRstValue[i];
        
		pstJsoncppExamp->stJsonExmpBody[i].sCpuRatio        = JSubJson["cpuRatio"].asString();
		pstJsoncppExamp->stJsonExmpBody[i].sServerIp        = JSubJson["serverIp"].asString();
		pstJsoncppExamp->stJsonExmpBody[i].sConNum          = JSubJson["conNum"].asString();
		pstJsoncppExamp->stJsonExmpBody[i].sWebsocketPort   = JSubJson["websocketPort"].asString();
		pstJsoncppExamp->stJsonExmpBody[i].sMqttPort        = JSubJson["mqttPort"].asString();
		pstJsoncppExamp->stJsonExmpBody[i].sTs              = JSubJson["TS"].asString();
 
		printf("cpuRatio = %s; serverIp = %s; conNum = %s; websocketPort = %s; mqttPort = %s; ts = %s\n",
                pstJsoncppExamp->stJsonExmpBody[i].sCpuRatio.c_str(), pstJsoncppExamp->stJsonExmpBody[i].sServerIp.c_str(),
                pstJsoncppExamp->stJsonExmpBody[i].sConNum.c_str(),   pstJsoncppExamp->stJsonExmpBody[i].sWebsocketPort.c_str(), 
                pstJsoncppExamp->stJsonExmpBody[i].sMqttPort.c_str(), pstJsoncppExamp->stJsonExmpBody[i].sTs.c_str());
	}
    
	return 0;
}
 
void Jsoncpp_CreateContent_Example(PT_Jsoncpp_Example pstJsoncppExamp)
{
    Json::Value JReq;
    Json::Value JJarray;
    Json::FastWriter JFastwriter;

    JReq["Result"]          = pstJsoncppExamp->s32Rst;
    JReq["ResultMessage"]   = pstJsoncppExamp->sRstMsg;

    for(unsigned int i = 0; i < sizeof(pstJsoncppExamp->stJsonExmpBody) / sizeof(T_JsoncppExmpBody); i++)
    {
        Json::Value JObject;
        JObject["cpuRatio"]         = pstJsoncppExamp->stJsonExmpBody[i].sCpuRatio;
        JObject["serverIp"]         = pstJsoncppExamp->stJsonExmpBody[i].sServerIp;
        JObject["conNum"]           = pstJsoncppExamp->stJsonExmpBody[i].sConNum;
        JObject["websocketPort"]    = pstJsoncppExamp->stJsonExmpBody[i].sWebsocketPort;
        JObject["mqttPort"]         = pstJsoncppExamp->stJsonExmpBody[i].sMqttPort;
        JObject["TS"]               = pstJsoncppExamp->stJsonExmpBody[i].sTs;
        
        JJarray.append(JObject);
    }

    JReq["ResultValue"] = JJarray;

    pstJsoncppExamp->sJContent = JFastwriter.write(JReq);

    printf("%s\n", pstJsoncppExamp->sJContent.c_str());

    Jsoncpp_WriteContent_ToFile((char *)"createJson.json", (const unsigned char* )pstJsoncppExamp->sJContent.c_str(), pstJsoncppExamp->sJContent.size());

}

int Jsoncpp_Test_Example(void)
{
    T_Jsoncpp_Example stJsoncppExamp;
	unsigned char* json = Jsoncpp_ReadContent_FromFile((char *)"parseJson.json");
	Jsoncpp_ParseContent_Example(&stJsoncppExamp, json);
	printf("===============================\n");
	Jsoncpp_CreateContent_Example(&stJsoncppExamp);

    delete [] json;
 
	return 0;
}

 主函数:main.cpp


#include 

extern "C"
{
#include 
}
using namespace std;

#include  

int main(int argc,char *argv[])
{
	Jsoncpp_Test_Example();
	
	return 0;
}

示例结果:

你可能感兴趣的:(ubuntu,交叉编译环境)