Json CPP 中文支持与入门示例
在每一个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();
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端