Json CPP Chinese support and getting started example, jsoncpp
Add the following to each Json Cpp built-in *. cpp file header:
#include "stdafx.h"
Modify the reference of the built-in header file in Json Cpp to single quotes. For example, the original code of json_reader.cpp is:
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>
After modification (note the differences between the referenced paths ):
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>
Go to json_reader.cpp row 87th and modify the Code as follows:
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}
Example of using JsonCpp:
The JSON code is as follows:
1 {2 "function": "add", 3 "host": "localhost", 4 "port": 8080,5 "method": "doUserAdd", 6 "varname ": "UserName" 7 "varvalue": "McDull" 8}
The C ++ code is as follows:
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();