在每一個Json Cpp自帶*.cpp文件頭加上:
#include "stdafx.h"
將Json Cpp對自帶的頭文件的引用修改為單引號方式,例如json_reader.cpp原始代碼為:
1 #include <json/reader.h> 2 #include <json/value.h> 3 #include <utility> 4 #include <cstdio> 5 #include <cassert> 6 #include <cstring> 7 #include <iostream> 8 #include <stdexcept>
修改後(注意我引用路徑的不同):
1 #include "stdafx.h" 2 #include "reader.h" 3 #include "value.h" 4 #include <utility> 5 #include <cstdio> 6 #include <cassert> 7 #include <cstring> 8 #include <iostream> 9 #include <stdexcept>
定位到json_reader.cpp第87行,將代碼修改為如下:
else if (cp <= 0xFFFF) { // add by sam BEGIN if((cp>=0x4E00 && cp<=0x9FA5)||(cp>0x9F00 && cp<0xFA2D)) { wchar_t src[2]={0}; char dest[5]={0}; src[0]=static_cast<wchar_t>(cp); std::string curLocale=setlocale(LC_ALL,NULL); setlocale(LC_ALL,"chs"); wcstombs_s(NULL,dest,5,src,2); result = dest; setlocale(LC_ALL,curLocale.c_str()); } else { result.resize(3); result[2] = static_cast<char>(0x80 | (0x3f & cp)); result[1] = 0x80 | static_cast<char>((0x3f & (cp >> 6))); result[0] = 0xE0 | static_cast<char>((0xf & (cp >> 12))); } // add by sam END }
使用JsonCpp例子:
JSON代碼如下
1 { 2 "function":"add", 3 "host":"localhost", 4 "port":8080, 5 "method":"doUserAdd", 6 "varname":"UserName" 7 "varvalue":"麥兜" 8 }
C++代碼如下
1 #include <string> 2 #include "Json.h" 3 ... 4 using namespace std; 5 ... 6 string strHost = root["host"].asString(); 7 int strPort = root["port"].asInt(); 8 string strMethod = root["method"].asString(); 9 string strFunc = root["function"].asString(); 10 string strVarName = root["varname"].asString(); 11 string strVarValue = root["varvalue"].asString();