{"id":24,"date":"2023-05-19T20:20:32","date_gmt":"2023-05-19T12:20:32","guid":{"rendered":"http:\/\/www.greatfar.cn\/?p=24"},"modified":"2023-06-01T15:37:48","modified_gmt":"2023-06-01T07:37:48","slug":"c-json%e8%a7%a3%e6%9e%90%e5%ba%93%ef%bc%9ajsoncpp%e3%80%81rapidjson","status":"publish","type":"post","link":"https:\/\/www.greatfar.cn\/archives\/24","title":{"rendered":"C++ JSON\u89e3\u6790\u5e93\uff1ajsoncpp\u3001rapidjson"},"content":{"rendered":"\n

{“errNum”:0,”retMsg”:”success”,”retData”:{“address”:”\\u56db\\u5ddd\\u7701\\u5185\\u6c5f\\u5e02\\u5a01\\u8fdc\\u53bf”,”sex”:”M”,”birthday”:”1990-12-26″}}<\/p>\n\n\n\n

\u6211\u4eec\u9700\u8981\u4ece\u4e2d\u63d0\u53d6\u51fa\u6027\u522b\uff0c\u5730\u5740\u548c\u51fa\u751f\u5e74\u6708\u7b49\u4fe1\u606f\u3002\u8fd9\u5c31\u9700\u8981json\u89e3\u6790\uff0c\u6211\u7528\u7684\u662f\u5f00\u6e90jsoncpp\u5e93\u3002<\/p>\n\n\n\n

    \n
  1. \u4e0b\u8f7d\u5730\u5740\uff1ahttp:\/\/sourceforge.net\/projects\/jsoncpp\/<\/a> \u6216\u8005https:\/\/github.com\/open-source-parsers\/jsoncpp<\/a> \uff1b\u4e0b\u8f7d\u540e\u89e3\u538b\u3002<\/li>\n\n\n\n
  2. \u6709\u4e24\u79cd\u4f7f\u7528\u65b9\u6cd5\uff0c\u4e00\u79cd\u662f\u81ea\u5df1\u7f16\u8bd1\u6210lib\u9759\u6001\u5e93<\/strong>\uff0c\u53e6\u5916\u4e00\u79cd\u662f\u76f4\u63a5\u4f7f\u7528\u6e90\u4ee3\u7801<\/strong>\u3002<\/li>\n<\/ol>\n\n\n\n

    \u81ea\u5df1\u7f16\u8bd1\u662f\u5728\/makefiles\/vs71\u76ee\u5f55\uff0cVS\u6253\u5f00\u7f16\u8bd1\u5373\u53ef\u3002<\/p>\n\n\n\n

    \"\"<\/figure>\n\n\n\n

    \u4f46\u662f\u6211\u7f16\u8bd1\u540e\u4f7f\u7528\u53d1\u73b0\u9519\u8bef\uff0cLNK2038 RuntimeLibrary \u4e0d\u5339\u914d\u4ec0\u4e48\u7684\uff0c\u8fd8\u6709\u5e93\u4e0e\u5fae\u8f6f\u9ed8\u8ba4\u5e93\u51b2\u7a81\uff0c\u540e\u6765\u8fd8\u662f\u7528\u7684\u6e90\u4ee3\u7801\u3002<\/p>\n\n\n\n

    \u6ce8\u610f\u4e8b\u9879:<\/p>\n\n\n\n

    1.\u5982\u679c\u81ea\u5df1\u7f16\u8bd1dll\uff0c\u90a3\u4e2a\u7248\u672c\u7684VS\u7f16\u8bd1\u7684\u5e93\u5c31\u9700\u8981\u54ea\u4e2a\u7248\u672c\u7684VS\u4f7f\u7528\uff0c\u5176\u4ed6\u7248\u672c\u4f7f\u7528\u4f1a\u51fa\u9519\uff1b<\/p>\n\n\n\n

    2.C\/C++-\u4ee3\u7801\u751f\u6210-\u8fd0\u884c\u5e93 \u8fd9\u91cc\u9009\u9879\u9700\u8981\u6ce8\u610f\uff0c\u7f16\u8bd1\u65f6\u4ec0\u4e48\u9009\u62e9\uff0c\u4f7f\u7528\u4e5f\u5e94\u8be5\u662f\u4e00\u6837\u7684\u9009\u62e9\uff1b<\/p>\n\n\n\n

    3. \u76f4\u63a5\u6e90\u7801\u6709\u4e24\u4e2a\u76ee\u5f55\u9700\u8981\u62f7\u8d1d\uff0c\u4e00\u4e2a\u662f\/src\/lib_json\uff0c\u76f4\u63a5\u62f7\u8d1d\u6587\u4ef6\u5230\u5de5\u7a0b\u4e0b\u5373\u53ef\uff0c\u53e6\u5916\u4e00\u4e2a\u662f\/include\/json\u4e0b\u6587\u4ef6\uff0c\u6b64\u65f6\u6ce8\u610f\u8981\u6587\u4ef6\u5939json\u4e00\u8d77\u62f7\u8d1d\u3002<\/p>\n\n\n\n

    \/#include json\/value.h<\/p>\n\n\n\n

    \u53ef\u80fd\u9700\u8981\u6539\u4e3a\uff1a<\/p>\n\n\n\n

    #include “json\/value.h”<\/p>\n\n\n\n

    \u5de5\u7a0b\u76ee\u5f55\u7ed3\u6784\uff1a<\/p>\n\n\n\n

    \"0\"\/<\/figure>\n\n\n\n

    jsoncpp\u7684include\u8def\u5f84\u4e3a\uff1a<\/p>\n\n\n\n

    \"0\"\/<\/figure>\n\n\n\n

    jsoncpp\u5b98\u65b9\u6587\u6863\uff1a<\/p>\n\n\n\n

    http:\/\/open-source-parsers.github.io\/jsoncpp-docs\/doxygen\/index.html<\/a><\/p>\n\n\n\n

    MFC\u8c03\u7528\u793a\u4f8b\uff1a\uff08\u6700\u597d\u81ea\u5df1\u53c2\u770b\u5b98\u65b9\u6587\u6863\uff0c\u793a\u4f8b\u4e0d\u4e00\u5b9a\u9002\u7528\uff09<\/p>\n\n\n\n

    #include <afxinet.h> #include “json.h” void CTestJsonDlg::OnBnClickedButton1() { \/\/\u4f7f\u7528\u767e\u5ea6api\u83b7\u53d6\u6570\u636e—————————————————————– char buf[1000] = { 0 }; CString StrDate, strValue; CInternetSession session; CHttpFile* file = NULL; int nRead; file = (CHttpFile *)session.OpenURL(_T(“http:\/\/apistore.baidu.com\/microservice\/icardinfo?id=xxxxxxxxxxxxxx”)); if (NULL != file){ \/\/Do something here with the web request \/\/Clean up the file here to avoid a memory leak!!!!!!! while ((nRead = file->Read(buf, sizeof(buf))) > 0){ StrDate = buf; } GetDlgItem(IDC_ID_CARD)->SetWindowText(StrDate); file->Close(); delete file; } session.Close(); \/\/\u89e3\u6790json\u6570\u636e———————————————————————– CString strTemp; std::string value; Json::Reader reader; Json::Value root; if (reader.parse(buf, root)) \/\/ reader\u5c06Json\u5b57\u7b26\u4e32\u89e3\u6790\u5230root\uff0croot\u5c06\u5305\u542bJson\u91cc\u6240\u6709\u5b50\u5143\u7d20 { int errNum = root[“errNum”].asInt();\/\/ \u8bbf\u95ee\u8282\u70b9\uff0cerrNum = 0 std::string retMsg = root[“retMsg”].asString(); \/\/ \u8bbf\u95ee\u8282\u70b9\uff0cretMsg = “success” strTemp = retMsg.c_str(); MessageBox(strTemp); Json::Value list = root[“retData”]; \/\/\u5305\u542baddresss,sex,birthday if (!list.isNull()){ \/\/ int count = list.size(); Json::Value::Members members(list.getMemberNames()); \/\/ std::sort(members.begin(), members.end()); \/\/\u89e3\u6790\u540e\u9762\u7684\u5d4c\u5957json\u641e\u4e86\u534a\u5929\uff0c\u6700\u540e\u624d\u5728\u6d4b\u8bd5\u4ee3\u7801\u4e2d\u53d1\u73b0\u89e3\u51b3\u65b9\u6848\u3002 for (auto it = members.begin(); it != members.end();++it) { const std::string &name = *it; std::string str = list[name].asCString(); \/\/ strTemp = str.c_str(); strTemp = UTF8ToUnicode(const_cast<char *>(str.c_str())); MessageBox(strTemp); } } } }<\/p>\n\n\n\n

    5 \u6ce8\u610f\u4e8b\u9879\uff1a<\/p>\n\n\n\n

    \u4e00\u4e2a\u9700\u8981\u6ce8\u610f\u7684\u5730\u65b9\u662f\u7f16\u7801\u95ee\u9898\uff0c\u7531\u4e8e\u5730\u5740\u662f\u4e2d\u6587\uff0c\u6240\u4ee5\u9700\u8981\u8fdb\u884cUTF8ToUnicode\u8f6c\u7801\u3002<\/p>\n\n\n\n

    \u8fd8\u6709\u6dfb\u52a0\u7684\u5934\u6587\u4ef6\u9700\u8981\u53bb\u6389\u6dfb\u52a0\u9884\u5904\u7406\u5934\u9009\u9879\uff0c\u6216\u8005#include \u201cStdAfx.h\u201d\u3002<\/p>\n\n\n\n

    \u5176\u4ed6Error:<\/p>\n\n\n\n

      \n
    1. json_value.asm\u201d: No such file or directory<\/li>\n\n\n\n
    2. jsoncpp fatal error link1257<\/li>\n<\/ol>\n\n\n\n

      \u7f16\u8bd1\u5e93\u6587\u4ef6\u65f6\uff0c\u914d\u7f6e\u5c5e\u6027-\u5e38\u89c4-\u5168\u7a0b\u5e8f\u4f18\u5316 \u65e0\u5168\u7a0b\u5e8f\u4f18\u5316\u3002<\/p>\n\n\n\n

      C\/C++ -\u8f93\u51fa\u6587\u4ef6 – \u6c47\u7f16\u7a0b\u5e8f\u8f93\u51fa\uff1a\u65e0\u5217\u8868\u3002<\/p>\n\n\n\n

      \u5373\u53ef\u3002<\/p>\n\n\n\n

      https:\/\/blog.csdn.net\/a379039233\/article\/details\/49424393<\/a><\/p>\n\n\n\n

      —————————————————-<\/p>\n\n\n\n

      https:\/\/www.jianshu.com\/p\/017c13a65622<\/a><\/p>\n\n\n\n

      \u4ee5\u524dVC\u89e3\u6790JSON\u7528\u7684\u662f\u7b2c\u4e09\u65b9\u5e93JSONCpp\uff0c\u4f46\u662fJSONCpp\u5bf9\u6211\u73b0\u5728\u8981\u505a\u7684\u9879\u76ee\u6765\u8bf4\u6709\u7f3a\u9677\uff0c\u4e0d\u652f\u6301Unicode\u7684CString\u89e3\u6790\uff0c\u8981\u7ecf\u8fc7\u7e41\u7410\u7684\u8f6c\u5316\uff0c\u7f51\u4e0a\u4e5f\u7f3a\u5c11\u7528\u7684\u4fe1\u606f\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002\u73b0\u5728\u7528RapidJson\u8bd5\u7740\u6765\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n\n\n\n

      \u7b80\u4ecb<\/p>\n\n\n\n

      RapidJson<\/strong>\u662f\u817e\u8baf<\/strong>\u63a8\u51fa\u7684\u8f7b\u91cf\u7ea7\u7684\u9ad8\u6548\u7684 C++ JSON \u89e3\u6790\uff0f\u751f\u6210\u5668\u3002\u8bf4\u4ed6\u9ad8\u6548\u80af\u5b9a\u6709\u4e9b\u4eba\u4e0d\u670d\uff0c\u90a3\u4e48\u6709\u591a\u9ad8\u6548\u5462\uff1f\u8fd9\u91cc\u6709\u4ed6\u7684\u4f5c\u8005\u5bf9 28 \u4e2a C\/C++ JSON \u5e93\u7684\u6bd4\u8f83<\/a>\uff0c\u5373\u4fbf\u662f\u8001\u724c\u7684JsonCpp\u5e93\u5728\u6548\u7387\u4e0a\u4f9d\u7136\u8ddf\u4ed6\u5dee\u4e86\u4e00\u4e2a\u91cf\u7ea7\u3002\u66f4\u8ba9\u6211\u770b\u91cd\u7684\u662f\u4ed6\u4e0b\u9762\u8fd9\u4e2a\u4f18\u70b9<\/p>\n\n\n\n

      http:\/\/rapidjson.org\/zh-cn\/<\/a><\/p>\n\n\n\n

      \"0\"\/<\/figure>\n\n\n\n

      https:\/\/github.com\/Tencent\/rapidjson<\/a><\/p>\n\n\n\n

      RapidJSON \u662f\u4e00\u4e2a C++ \u7684 JSON \u89e3\u6790\u5668\u53ca\u751f\u6210\u5668\u3002\u5b83\u7684\u7075\u611f\u6765\u81ea RapidXml<\/a>\u3002<\/p>\n\n\n\n