jsoncpp使用小結

一、jsoncpp介紹

        jsoncpp是一個開源C++庫,提供對JSON字符串序列化/反序列化的功能。

開源地址:https://github.com/open-source-parsers/jsoncpp

文檔地址:http://open-source-parsers.github.io/jsoncpp-docs/doxygen/index.html
 

二、jsoncpp的使用

        jsoncpp主要包含三種類型的C++類 - value、reader、writer。value表示json對象和數組。reader用於反序列化json字符串。writer用於序列化json字符串。簡單使用示例:

示例1:生成json

Json::Value jsonRoot; //定義根節點
Json::Value jsonItem; //定義一個子對象
jsonItem["item1"] = "one"; //添加數據
jsonItem["item2"] = 2;
jsonRoot.append(jsonItem);
jsonItem.clear(); //清除jsonItemjsonItem["item1.0"] = 1.0;
jsonItem["item2.0"] = 2.0;
jsonRoot["item"] = jsonItem;
std::string strJson = jsonRoot.toStyledString(); //json結果

示例2:讀取json

Json::Reader reader;
string json_document = "{\"age\" : 123, \"name\" : \"weng\"}";
if (!reader.parse(json_document, json_object))
{
     cout << "error" << endl;
     return 0;
 }

else
{
   cout <<"age:" <<json_object["age"] << " name" << json_object["name"] << endl;
}

示例3:空數組

Json::Value root;  
root["FaceItemObjects"].resize(0);  
std::string strJson = root.toStyledString();

示例4:序列化複雜json字符串

Json::Value root;
Json::Value itemFaceArray;
Json::value itemFace;
root["errorCode"] = 0;
root["faces"].resize(0);
//face1
itemFace["x"]=1;
itemFace["y"]=1;
itemFace["width"]=100;
itemFace["height"]=100;
itemFace["score"]=95;
itemFaceArray.append(itemFace);
//face2
itemFace.clear();
itemFace["x"]=200;
itemFace["y"]=200;
itemFace["width"]=100;
itemFace["height"]=100;
itemFace["score"]=98;
itemFaceArray.append(itemFace);
root["faces"] = itemFaceArray;
std::string strJson = root.toStyledString();

三、注意事項

1、jsoncpp不支持int64位的。

解決方法1:需要0.6.0rc2版本。 不要採用0.5.0版本,因爲它沒有64位int。

解決方法2:0.5.0版本基礎上修改代碼支持64位int。具體參考https://sourceforge.net/p/jsoncpp/discussion/483465/thread/3610921c/

解決方法3:asUInt換成asDouble
 

Json::Reader reader;
Json::Value root;
if (reader.parse(str, root))
{
//獲取裏面內容
OutputDebugString(_T("STRING TO JSON \n"));
//std::string str1 = root["messageType"].asString();
long long tmstamp = ((long long)(root["sendTime"].asUInt()))/1000;
WCHAR* wstr = NULL;
TimestampToLocalTime(&wstr,tmstamp);
}

結果發現第8行會出錯,查了下錯誤原因, 原來SendTime是一個一毫秒爲單位的時間戳,其值爲1403575350411,這個值的大小遠遠超出了 unsigned int 或者 int的最大值,只能用INT64來表示, 但是看看Json::Value裏面的函數只有asInt, asUint,沒有取64位整數的函數,那怎麼辦呢?裏面雖然沒有64位的但是有一個asDouble,duoble的指數爲11位,能表示的範圍比Int64還大,所以上面的asUInt換成asDouble就可以了。

解決方法4:使用高版本(比如1.8.4),在使用jsoncpp庫的工程屬性預定義中增加JSON_HAS_INT64

2、獲取json中不存在名稱的值,並且作爲asCString()拷貝時,程序會core。如strncpy(a, root["test"].asCString(), sizeof(a));

3、解決jsoncpp中文輸出爲unicode格式的"\u"、VS讀取utf8格式中文輸出亂碼問題。
參考:https://blog.csdn.net/wgxh05/article/details/82792729

4、fatal error C1083: 無法打開編譯器生成的文件:“../../build/vs71/release/lib_json\json_value.asm”: No such file or directory 

解決方法:修改生成靜態庫文件的工程的屬性:路徑爲:菜單---項目--屬性---配置屬性---c/c++---輸出文件---彙編程序輸出:無列表

5、異常捕獲

bool bReadJsonSuccess = false;
try
{
    Json::Reader reader;
    string json_document = "{\"age\" : 123, \"name\" : \"weng\"}";
    if (!reader.parse(json_document, json_object))
    {
            cout << "error" << endl;
            bReadJsonSuccess = false;
    }
    else
    {
          cout <<"age:" <<json_object["age"] << " name" << json_object["name"] << endl;
          bReadJsonSuccess = true;
    }
}
catch(...)
{
    bReadJsonSuccess = false;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章