C++jsocpp構造体データをjson文字列/json文字列に変換して構造体データに変換します。

5824 ワード

//jsoncpp使用方法まとめ/構造体データをjson文字列に変換/json文字列を構造体データに変換する
 
//jsocppダウンロードアドレス http://sourceforge.net/projects/jsoncpp/
 
//サンプルコードは以下の通りです。
#include 
#include 
#include 
#include 
#include 
#include "json/json.h"
 
//         
typedef struct {
    int nNum;
    char szFile[20];
}TEST_ST_FILE;
 
typedef struct {
    char szMemo[20];
    int nCount;
    TEST_ST_FILE szFileList[5];
}TEST_ST_FILE_LIST;
 
//         JSON   
int StructDataToJsonString(TEST_ST_FILE_LIST *pStructData, char *pJsonData);
// JSON           
int JsonStringToStructData(char *pJsonData, TEST_ST_FILE_LIST *pStructData);
 
//    
int main(int argc, char *argv[])
{
    TEST_ST_FILE_LIST stFileList;
    char szJsonData[4096];
 
    memset(&stFileList, 0, sizeof(TEST_ST_FILE_LIST));
    memset(szJsonData, 0, sizeof(szJsonData));
 
    //     
    strncpy(stFileList.szMemo, "jsoncppmemo", sizeof(stFileList.szMemo));
    stFileList.nCount = 5;
    for(int i = 0; i < stFileList.nCount; i++)
    {
        stFileList.szFileList[i].nNum = i + 1;
        sprintf(stFileList.szFileList[i].szFile, "file%d", i + 1);
    }
    printf("struct data to json string.
");     printf("memo:%s count:%d.
", stFileList.szMemo, stFileList.nCount);     for(int i = 0; i < stFileList.nCount; i++)     {         printf("num:%d file:%s.
", stFileList.szFileList[i].nNum,              stFileList.szFileList[i].szFile);     }       // JSON     StructDataToJsonString(&stFileList, szJsonData);     printf("json string:%s.
", szJsonData);       // JSON     memset(&stFileList, 0, sizeof(TEST_ST_FILE_LIST));     JsonStringToStructData(szJsonData, &stFileList);     printf("json string to struct data.
");     printf("memo:%s count:%d.
", stFileList.szMemo, stFileList.nCount);     for(int i = 0; i < stFileList.nCount; i++)     {         printf("num:%d file:%s.
", stFileList.szFileList[i].nNum,              stFileList.szFileList[i].szFile);     }     return 0; }   // JSON int StructDataToJsonString(TEST_ST_FILE_LIST *pStructData, char *pJsonData) {     try     {         Json::Value root;         Json::Value arrayObj;         Json::Value item;           root["memo"] = pStructData->szMemo;         root["file_count"] = pStructData->nCount;         // file_list         for(int i = 0; i < pStructData->nCount; i ++)         {             item["num"] = pStructData->szFileList[i].nNum;             item["file"] = pStructData->szFileList[i].szFile;             arrayObj.append(item);         }         root["file_list"] = arrayObj;         // JSON JSON ( )         std::string strOut = root.toStyledString();         //  JSON JSON ( )         //Json::FastWriter writer;         //std::string strOut = writer.write(root);           strcpy(pJsonData, strOut.c_str());     }     catch(std::exception &ex)     {         printf("StructDataToJsonString exception %s.
", ex.what());         return -1;     }     return 0; }   // JSON int JsonStringToStructData(char *pJsonData, TEST_ST_FILE_LIST *pStructData) {     try     {         bool bRet = false;         std::string strTemp;         Json::Reader reader;         Json::Value value;         Json::Value arrayObj;         Json::Value item;           // JSON JSON         bRet = reader.parse(pJsonData, value);         if(bRet == false)         {             printf("JsonStringToStructData reader parse error.
");             return -1;         }         // memo         bRet = value["memo"].empty();         if(bRet == true)         {             printf("JsonStringToStructData memo is not exist.
");             return -1;         }         strTemp = value["memo"].asString();         strncpy(pStructData->szMemo, strTemp.c_str(), sizeof(pStructData->szMemo));         // file_count         bRet = value["file_count"].empty();         if(bRet == true)         {             printf("JsonStringToStructData file_count is not exist.
");             return -1;         }         pStructData->nCount = value["file_count"].asInt();           // file_list         arrayObj = value["file_list"];         pStructData->nCount = arrayObj.size();         for(int nIdx = 0; nIdx < pStructData->nCount; nIdx++)         {             item = arrayObj[nIdx];             // num             pStructData->szFileList[nIdx].nNum = item["num"].asInt();             // file             strTemp = item["file"].asString();             strncpy(pStructData->szFileList[nIdx].szFile, strTemp.c_str(),                  sizeof(pStructData->szFileList[nIdx].szFile));         }     }     catch(std::exception &ex)     {         printf( "JsonStringToStructData exception %s.
", ex.what());         return -1;     }     return 0; }
 
執行結果struct data to json string.memo:json cppmemo count:5.num:1 file:file 1.num:2 file:file 2.num:3 file:file 3.num:4 file:4.num:5 file:5
json string:{ 「fileucount」:5、 「file list」:[ {  「file」:「file 1」、  「num」:1 },  {  「file」:「file 2」、  「num」:2 },  {  「file」:「file 3」、  「num」:3 },  {  「file」:「file 4」、  「num」:4 },  {  「file」:「file 5」、  「num」:5 }  ],  「memo」:「jsocppmemo」。
json string to struct data.memo:json cppmemo count:5.num:1 file:file 1.num:2 file:file:file 2.num:3 file:file 3.num:4 file:4.num:5 file:file:5.-------------------  作者:テックス  ソース:CSDN  原文:https://blog.csdn.net/dgyanyong/article/details/16985235  本文はブロガーのオリジナル文章です。転載はブログのリンクを添付してください。