小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

C 解析Json——jsoncpp

 西北望msm66g9f 2018-07-07

JSON(JavaScript Object Notation) 是一種輕量級(jí)的數(shù)據(jù)交換格式,和xml類似,本文主要對(duì)VS2008中使用Jsoncpp解析json的方法做一下記錄。
Jsoncpp是個(gè)跨平臺(tái)的開(kāi)源庫(kù),下載地址:http:///projects/jsoncpp/。

 


方法一:使用Jsoncpp生成的lib文件

 

      解壓上面下載的Jsoncpp文件,在jsoncpp-src-0.5.0/makefiles/vs71目錄里找到j(luò)soncpp.sln,用VS2008版本編譯,默認(rèn)生成靜態(tài)鏈接庫(kù)。 在工程中引用,只需要包含include/json下的頭文件及生成的.lib文件即可。
      如何包含lib文件:在.cpp文件中#pragma comment(lib.'json_vc71_libmt.lib'),在工程屬性中Linker下Input中Additional Dependencies寫(xiě)入lib文件名字(Release下為json_vc71_libmt.lib,Debug為json_vc71_libmtd.lib)

 


注意:Jsoncpp的lib工程編譯選項(xiàng)要和VS工程中的編譯選項(xiàng)保持一致。如lib文件工程編譯選項(xiàng)為MT(或MTd),VS工程中也要選擇MT(或MTd),否則會(huì)出現(xiàn)編譯錯(cuò)誤問(wèn)題,debug和release下生成的lib文件名字不同,注意不要看錯(cuò)了,當(dāng)成一個(gè)文件來(lái)使用(我就犯了這個(gè)錯(cuò)誤)。

方法二:使用Jsoncpp包中的.cpp和.h文件
      解壓上面下載的Jsoncpp文件,把jsoncpp-src-0.5.0文件拷貝到工程目錄下,將jsoncpp-src-0.5.0\jsoncpp-src-0.5.0\include\json和jsoncpp-src-0.5.0\jsoncpp-src-0.5.0\src\lib_json目錄里的文件包含到VS工程中,在VS工程的屬性C/C++下General中Additional Include Directories包含頭文件目錄.\jsoncpp-src-0.5.0\include。在使用的cpp文件中包含json頭文件即可,如:#include 'json/json.h'。將json_reader.cpp、json_value.cpp和json_writer.cpp三個(gè)文件的Precompiled Header屬性設(shè)置為Not Using Precompiled Headers,否則編譯會(huì)出現(xiàn)錯(cuò)誤。

jsoncpp 使用詳解

jsoncpp 主要包含三種類型的 class:Value、Reader、Writer。jsoncpp 中所有對(duì)象、類名都在 namespace Json 中,包含 json.h 即可。

Json::Value 只能處理 ANSI 類型的字符串,如果 C++ 程序是用 Unicode 編碼的,最好加一個(gè) Adapt 類來(lái)適配。


下面是從網(wǎng)上找的代碼示例:
1. 從字符串解析json

 

const char* str = '{\'uploadid\': \'UP000000\',\'code\': 100,\'msg\': \'\',\'files\': \'\'}';      Json::Reader reader;      Json::Value root;      if (reader.parse(str, root)) // reader將Json字符串解析到root,root將包含Json里所有子元素      {          std::string upload_id = root['uploadid'].asString(); // 訪問(wèn)節(jié)點(diǎn),upload_id = 'UP000000'   int code = root['code'].asInt(); // 訪問(wèn)節(jié)點(diǎn),code = 100 }

 

2. 從文件解析json

int ReadJsonFromFile(const char* filename)  {      Json::Reader reader;// 解析json用Json::Reader   Json::Value root; // Json::Value是一種很重要的類型,可以代表任意類型。如int, string, object, array            std::ifstream is;      is.open (filename, std::ios::binary );        if (reader.parse(is, root, FALSE))      {          std::string code;          if (!root['files'].isNull()) // 訪問(wèn)節(jié)點(diǎn),Access an object value by name, create a null member if it does not exist.   code = root['uploadid'].asString();                  code = root.get('uploadid', 'null').asString();// 訪問(wèn)節(jié)點(diǎn),Return the member named key if it exist, defaultValue otherwise.     int file_size = root['files'].size(); // 得到'files'的數(shù)組個(gè)數(shù)   for(int i = 0; i < file_size;="" ++i)="">// 遍歷數(shù)組          {              Json::Value val_image = root['files'][i]['images'];              int image_size = val_image.size();              for(int j = 0; j < image_size;="">j)              {                  std::string type = val_image[j]['type'].asString();                  std::string url = val_image[j]['url'].asString();                printf('type : %s, url : %s \n', type.c_str(), url.c_str());            }          }      }      is.close();      return 0;  }

 

3. 向文件中插入json

 

void WriteJsonData(const char* filename){    Json::Reader reader;      Json::Value root; // Json::Value是一種很重要的類型,可以代表任意類型。如int, string, object, array            std::ifstream is;      is.open (filename, std::ios::binary );        if (reader.parse(is, root))      {          Json::Value arrayObj;   // 構(gòu)建對(duì)象          Json::Value new_item, new_item1;          new_item['date'] = '2011-11-11';          new_item1['time'] = '11:11:11';          arrayObj.append(new_item);  // 插入數(shù)組成員   arrayObj.append(new_item1); // 插入數(shù)組成員   int file_size = root['files'].size();          for(int i = 0; i < file_size;="">i)              root['files'][i]['exifs'] = arrayObj; // 插入原json中 std::string out = root.toStyledString();          // 輸出無(wú)格式j(luò)son字符串          Json::FastWriter writer;          std::string strWrite = writer.write(root);        std::ofstream ofs;        ofs.open('test_write.json');        ofs strWrite;        ofs.close();    }      is.close();  }

 4.序列化json字符串

先構(gòu)建一個(gè)Json對(duì)象,此Json對(duì)象中含有數(shù)組,然后把Json對(duì)象序列化成字符串,代碼如下:    Json::Value root;    Json::Value arrayObj;    Json::Value item;    for (int i=0; i10; i++)    {      item['key'] = i;      arrayObj.append(item);    }    root['key1'] = “value1″;    root['key2'] = “value2″;    root['array'] = arrayObj;    root.toStyledString();    std::string out = root.toStyledString();    std::cout <>out std::endl;

 

5.反序列化json

比如一個(gè)Json對(duì)象的字符串序列如下,其中”array”:[...]表示Json對(duì)象中的數(shù)組:{“key1″:”value1″,”array”:[{'key2':'value2'},{'key2':'value3'},{'key2':'value4'}]},那怎么分別取到key1和key2的值呢,代碼如下所示:    std::string strValue = “{\”key1\”:\”value1\”,\”array\”:[{\'key2\':\'value2\'},{\'key2\':\'value3\'},{\'key2\':\'value4\'}]}”;    Json::Reader reader;    Json::Value value;    if (reader.parse(strValue, value))    {      std::string out = value['key1'].asString();      std::cout <>out std::endl;      const Json::Value arrayObj = value['array'];      for (int i=0; i)      {        out = arrayObj[i]['key2'].asString();        std::cout <>out;        if (i != arrayObj.size() – 1 )          std::cout std::endl;      }    }

6.刪除json子對(duì)象

   std::string strContent = '{\'key\':\'1\',\'name\':\'test\'}';    Json::Reader reader;    Json::Value value;    if (reader.parse(strContent, value))    {        Json::Value root=value;        root.removeMember('key');        printf('%s \n',root.toStyledString().c_str());
  }

 7. 利用jsoncpp將json字符串轉(zhuǎn)換為Vector

在API測(cè)試過(guò)程中經(jīng)常會(huì)遇到傳入?yún)?shù)為復(fù)雜類型,一般情況下在python下,習(xí)慣用字典來(lái)表示復(fù)雜類型。但是c++對(duì)字符串的處理是比較弱智的,一般c++里邊會(huì)用vector來(lái)存儲(chǔ)復(fù)雜類型,那么就存在轉(zhuǎn)換的問(wèn)題,下面小段代碼記錄了將字符串轉(zhuǎn)換為Vector的過(guò)程

待轉(zhuǎn)換的字符串如下:

const char * jsongroupinfo='[{/'groupId/' :946838524,/'groupname/' :/'bababa/', /'mask/':1,/'parentid/':946755072}]'; Json::Reader reader;Json::Value json_object;if (!reader.parse(jsongroupinfo, json_object))  return 'parse jsonstr error';SUserChggroup sucg;VECTOR< suserchggroup=""> m_groupInfo;for(int i = 0; i < json_object.size();="" i="">){  Json::Value ¤t = json_object[i];  sucg.m_groupId = current['groupId'].asInt();  sucg.m_groupName = current['groupname'].asString();  sucg.m_mask = current['mask'].asInt();  sucg.m_parentId = current['parentid'].asInt();  m_groupInfo.push_back(sucg);}

 

 

簡(jiǎn)而言之,就是把它變成解析成一個(gè)個(gè)對(duì)象,再將對(duì)象存儲(chǔ)到vector中。

 

ps:在使用vs2005調(diào)用vs2010編譯的dll時(shí)候,出現(xiàn)string的內(nèi)存錯(cuò)誤,在不同版本的string的不能相互傳遞,用const char *比較好,相關(guān)代碼修改:

   std::string strRtn = '{'success':true,'user':{'id':6,'username':'wq','type':'admin','membership':{'type':'paid','expiredAt':'2019-07-28T16:00:00.000Z'}},'token':'8de57200-3235-11e8-83f1-9739d2f0386f'}';    std::string strToken;    Json::Reader reader;                                    //解析json用Json::Reader Json::Value value; //可以代表任意類型 if (reader.parse(strRtn.c_str(),strRtn.c_str()+strRtn.size(), value))      {          if (value['success'].asBool())                {            strToken = value['token'].asCString();        }    }


    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多