前言
例如:学习rapidjson的解析和读写,然后发现在string下使用没有问题,但是换到CString后出现了很多问题,主要是中文的读写乱码问题。要解决中文读写,那么就需要处理rapidjson在UNICODE下的读写,程序里提供了wchar_t的操作,我们仅需要将SourceEncoding和TargetEncoding进行设置。
一、rapidjson是什么?
示例:rapidjson 是腾讯开源的一个高效的C++ JSON解析器及生成器。
二、使用步骤
1.引入库
代码如下(示例):源码:https://github.com/Tencent/rapidjson/
将所有的库里面的所有头文件添加到项目里就可以使用。
2.示例数据
{
"Info": [
{
"name": "钢管",
"code": "01010101",
"spec": "无缝钢管(SMLS)"
},
{
"name": "钢管",
"code": "01010102",
"spec": "直缝埋弧焊钢管(SAWL)"
},
{
"name": "钢管",
"code": "01010103",
"spec": "螺旋缝埋弧焊钢管(SAWH)"
},
{
"name": "钢管",
"code": "01010104",
"spec": "高频电阻焊管(HFW)"
},
{
"name": "弯头",
"code": "01020101",
"spec": "90°长半径弯头(R=1.5D)"
}
]
}
3.读入数据
代码如下:
#include "RapidJson/document.h"
#include "RapidJson/writer.h"
#include "RapidJson/prettywriter.h"
using namespace rapidjson;
typedef GenericDocument<UTF16<> > DocumentW;
typedef GenericValue<UTF16<> > ValueW;
std::map<CString, CString> CUtility::GetCodeByGuigeBiao()
{
map<CString, CString> mpInfo;
CString strFile = CUtility::GetAppPath() + _T("DL.json");
CStdioFile ReadF;
if (!ReadF.Open(strFile, CFile::modeRead))
{
return;
}
CString strTemp,strData;
while (ReadF.ReadString(strTemp))
{
strData += strTemp;
}
ReadF.Close();
delete temp;
DocumentW dom;
CString strType,strCode,strName,strGuige, strValue;
//DocumentW::AllocatorType& allocator = dom.GetAllocator();
if (!dom.Parse(strData).HasParseError())
{
if(dom.HasMember(_T("Info")) && dom[_T("Info")].IsArray())
{
const ValueW& arr = dom[_T("Info")];
size_t len = arr.Size();
for(size_t i = 0; i < len; i++)
{
const ValueW& object = arr[i];
//为防止类型不匹配,一般会添加类型校验
if(object.IsObject())
{
if(object.HasMember(_T("name")) && object[_T("name")].IsString())
{
strName = object[_T("name")].GetString();
}
if(object.HasMember(_T("code")) && object[_T("code")].IsString())
{
strCode = object[_T("code")].GetString();
}
if(object.HasMember(_T("spec")) && object[_T("spec")].IsString())
{
strGuige = object[_T("spec")].GetString();
}
strName.Replace(_T(" "), _T(""));
strGuige.Replace(_T(" "), _T(""));
if (strName.IsEmpty() && strGuige.IsEmpty())
{
continue;
}
if (strGuige.IsEmpty())
strValue = strName;
else
strValue = strName + _T(",") + strGuige;
mpInfo[strValue] = strCode;
}
}
}
}
return mpInfo;
}
4.写入数据
代码如下(示例):
#include "RapidJson/document.h"
#include "RapidJson/writer.h"
#include "RapidJson/prettywriter.h"
using namespace rapidjson;
typedef GenericStringBuffer<UTF16<> > StringBufferW;
bool CGetFiberNoBySpec::writeInfoToJson()
{
StringBufferW strBuf;
PrettyWriter<StringBufferW, UTF16<>, UTF16<> > writer(strBuf); //PrettyWriter是格式化的json(自动换行)
//rapidjson::Writer<rapidjson::StringBuffer> writer(strBuf);
writer.StartObject();
//5.5 结构体数组
writer.Key(_T("Info"));
writer.StartArray();
for (auto iter = m_Vec.begin(); iter != m_Vec.end(); ++iter)
{
CellInfo cell = *iter;
writer.StartObject();
writer.Key(_T("name"));
writer.String(cell.strName);
writer.Key(_T("code"));
writer.String(cell.strCode);
writer.Key(_T("spec"));
writer.String(cell.strSpec);
writer.EndObject();
}
writer.EndArray();
writer.EndObject();
return true;
}
后记
希望对遇到同样问题的人有所帮助。