首页 > 系统相关 >解决Windows下json.hpp中文乱码问题

解决Windows下json.hpp中文乱码问题

时间:2023-03-14 23:35:26浏览次数:61  
标签:pBuf string Windows hpp 乱码 json pwBuf str NULL

文中使用的是 json 库,整个库的代码由一个单独的头文件json.hpp组成,用普通的C++11编写的。它没有库,没有子项目,没有依赖关系,没有复杂的构建系统,使用起来很方便。

先引用头文件和命名空间:

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <Windows.h>
#include "json.hpp"

using namespace std;
using namespace nlohmann;

json.hpp默认使用UTF8编码,在有中文情况下直接读取string会乱码,需要提供UTF8和string互相转换的函数

std::string UTF8_To_string(const std::string& str)
{
    int nwLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, NULL, 0);    
    wchar_t* pwBuf = new wchar_t[nwLen + 1];//加1用于截断字符串 
    memset(pwBuf, 0, nwLen * 2 + 2);

    MultiByteToWideChar(CP_UTF8, 0, str.c_str(), str.length(), pwBuf, nwLen);

    int nLen = WideCharToMultiByte(CP_ACP, 0, pwBuf, -1, NULL, NULL, NULL, NULL);

    char* pBuf = new char[nLen + 1];
    memset(pBuf, 0, nLen + 1);

    WideCharToMultiByte(CP_ACP, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);

    std::string retStr = pBuf;

    delete[]pBuf;
    delete[]pwBuf;

    pBuf = NULL;
    pwBuf = NULL;

    return retStr;
}

std::string string_To_UTF8(const std::string& str)
{
    int nwLen = ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);

    wchar_t* pwBuf = new wchar_t[nwLen + 1];//加1用于截断字符串 
    ZeroMemory(pwBuf, nwLen * 2 + 2);

    ::MultiByteToWideChar(CP_ACP, 0, str.c_str(), str.length(), pwBuf, nwLen);

    int nLen = ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, -1, NULL, NULL, NULL, NULL);

    char* pBuf = new char[nLen + 1];
    ZeroMemory(pBuf, nLen + 1);

    ::WideCharToMultiByte(CP_UTF8, 0, pwBuf, nwLen, pBuf, nLen, NULL, NULL);

    std::string retStr(pBuf);

    delete[]pwBuf;
    delete[]pBuf;

    pwBuf = NULL;
    pBuf = NULL;

    return retStr;
}

先定义一个file.json文件如下:

{
  "pi": 3.141,
  "happy": true,
  "name": "Niels中文",
  "nothing": null,
  "answer": {
    "everything": 42
  },
  "list": [1, 0, 2],
  "object": {
    "currency": "USD",
    "value": 42.99
  }
}

修改其中的name字段并保存,代码如下:

int main()
{
    // read a JSON file
    std::ifstream iStream("file.json");
    json jsonObj;
    iStream >> jsonObj;

    string strName = jsonObj["name"];
    string str = UTF8_To_string(strName);
    str = str + "测试";
    jsonObj["name"] = string_To_UTF8(str);

    // write prettified JSON to another file
    std::ofstream oStream("pretty.json");
    oStream << std::setw(4) << jsonObj << std::endl;

    return 0;
}

最后生成的pretty.json如下:

{
    "answer": {
        "everything": 42
    },
    "happy": true,
    "list": [
        1,
        0,
        2
    ],
    "name": "Niels中文测试",
    "nothing": null,
    "object": {
        "currency": "USD",
        "value": 42.99
    },
    "pi": 3.141
}

标签:pBuf,string,Windows,hpp,乱码,json,pwBuf,str,NULL
From: https://www.cnblogs.com/timefiles/p/17216905.html

相关文章

  • pip下载安装库、模块慢,配置windows全局的国内源
    步骤windows+r打开命令行;输入%appdata%回车;在出现的文件夹中找到名称为pip的文件夹(没有则创建)创建pip.ini文件[global]timeout=6000index-url=http://pypi.......
  • vue3仿windows弹窗
     一款基于vue3的仿windows弹窗。可以组件模板编写或函数式创建。地址:https://github.com/dnoyeb/box-win   安装npmadd'box-win'两种方式:1、组......
  • Windows11 Update
    PCHealthCheck==>BiosTPM2.0isDisabled;OpenmyBios->setting->Security->TrustedComputing->SecurityDeviceSupport->Enabled->f10(savesettings)Search"Win......
  • 如何在Windows环境下将Go程序以Linux环境编译
    前言因为最近有需要将项目编译成二进制文件上传到线上服务器进行部署的需求,发现Windows已经无法像之前切换GOOS来达到目的了,特此记录一下半天的成果。方案在一天的寻找......
  • windows系统mysql定时备份
    如下:一、创建bat任务脚本1.新建txt文档2.打开txt文档,并粘贴入以下内容3.按照自己的需求对内容进行修改,并删除掉//后内容以及中文空格,否则会运行失败4.保存,并将文件后......
  • DBeaver Ultimate Edtion 23 Multilingual (macOS, Linux, Windows) - 通用数据库工具
    请访问原文链接:https://sysin.org/blog/dbeaver-23/,查看最新版。原创作品,转载请保留出处。作者主页:www.sysin.org通用数据库工具DBeaver是一个通用的数据库管理工具,适......
  • Burp Suite Professional 2023.3 (macOS, Linux, Windows) - Web 应用安全、测试和扫
    BurpSuiteProfessional,Test,find,andexploitvulnerabilities.请访问原文链接:https://sysin.org/blog/burp-suite-pro-2023/,查看最新版。原创作品,转载请保留出处。......
  • 如何恢复电脑上删除的便签 如何恢复已删Windows10电脑便签
    如果你经常使用电脑办公的话,就会发现如果能够直接在电脑桌面上记录生活、工作上的各种事项是非常方便的。于是有不少Windows10电脑用户都会使用自带的便签软件,但是他们发......
  • windows计划任务 重启电脑
    右键我的电脑-管理-任务计划程序,点创建基本任务输入任务名称下一步,选择触发频率下一步,设好触发时间下一步,选择启动程序下一步选择执行的程序,关机重启的脚本路径为:C:\Wind......
  • 040.hive-hive建表注释中文乱码问题
     usehive;#mysql元数据库altertableCOLUMNS_V2modifycolumnCOMMENTvarchar(256)charactersetutf8;altertableTABLE_PARAMSmodifycolumnPARAM_VALUEvarc......