提要
生成的json文件在程序加载解析时出现上述错误,究其原因是生成json文件过程中编码问题。qt编译器默认的编码格式为utf-8,而windows一般为gbk编码,所以就需要在生成本地json文件的时候将utf-8编码转换为gbk编码。读取json文件的时候,将gbk编码转换为utf-8编码。
示例
json文件格式
{"btnInfo": [{"id": 10008,"index": 1,"name": "场景8"}, {"id": 10026,"index": 2,"name": "场景K-P蝴蝶飞"}, {"id": 10014,"index": 3,"name": "场景即"}, {"id": 10021,"index": 6,"name": "场景及对QP"}, {"id": 10021,"index": 7,"name": "场景及对QP"}]
}
生成json文件为本地编码。
void SystemScenenForm::createJsonFile()
{QJsonObject data;QJsonObject subData1;QJsonArray array;int index;QMap<int,stuSceNameId>::iterator it;for(it = m_btnInfoMap.begin(); it != m_btnInfoMap.end(); ++it){index = it.key();stuSceNameId temp = *it;subData1.insert("index",index);subData1.insert("id",temp.id);subData1.insert("name",QString::fromLocal8Bit(temp.name.toLocal8Bit()));//将utf-8转换为gbkarray.append(subData1);}data.insert("btnInfo",array);QJsonDocument document;document.setObject(data);QByteArray jsonArray = document.toJson(QJsonDocument::Compact);QString strJson(jsonArray);QString strName = QCoreApplication::applicationDirPath() + "/sceneBtnInfo.json";QFile file(strName);if(!file.open(QIODevice::WriteOnly | QIODevice::Text)){QString strTitle = "提示";QString strContent = QString("%1文件打开失败!").arg(strName);QMessageBox::information(this,strTitle,strContent);return ;}QTextStream in(&file);//生成的json文件中编码为gbkin<<strJson;file.close();
}
读取json文件时将本地编码进行转换为gbk编码。
QString SystemScenenForm::readSceneBtnInfo(QString fileName)
{QByteArray allData;
#ifdef OUT_PUToutPut<<"文件路径及名称:"<<fileName;
#endifQTextCodec *code = QTextCodec::codecForName("GBK");QFile file(fileName);if (!file.open(QIODevice::ReadOnly)){
#ifdef OUT_PUToutPut<<QString("%1配置文件读取失败!").arg(fileName);
#endifreturn allData;}allData = file.readAll();QString str = code->toUnicode(allData);//gbk编码的QByteArray类型变量转换为utf-8编码的QString变量file.close();
#ifdef OUT_PUT
// outPut<<"json文件内容:"<<allData;
#endifreturn str;
}void SystemScenenForm::parseSceneBtnJson(QByteArray &byteArray)
{int tempIndex;stuSceNameId tempStu;if(m_btnInfoMap.size() != 0){m_btnInfoMap.clear();}QJsonObject jsonObject;QJsonParseError jsonError;QJsonDocument document = QJsonDocument::fromJson(byteArray,&jsonError);if(!document.isEmpty() && jsonError.error == QJsonParseError::NoError){QJsonObject resultObject = document.object();QJsonValue jsonValue = resultObject.value("btnInfo");if(jsonValue.isArray()){QJsonArray jsonArray = jsonValue.toArray();for(int i = 0; i < jsonArray.size(); ++i){jsonObject = jsonArray.at(i).toObject();tempIndex = jsonObject.value("index").toInt();tempStu.id = jsonObject.value("id").toInt();tempStu.name = jsonObject.value("name").toString();m_btnInfoMap.insert(tempIndex,tempStu);}}}else{outPut<<"sceneBtnInfo.json文件解析失败"<<jsonError.errorString();}
}//读取json文件时函数的调用,调用的为上面的函数
QString byteArray = readSceneBtnInfo(strName);
outPut<<"读到的字符串:"<<byteArray;
if(byteArray.size() == 0)
{return ;
}
QByteArray array = byteArray.toUtf8();//将utf-8编码的QString类型变量转换为utf-8的QByteArray类型变量
parseSceneBtnJson(array);
这样就解决了生成含有中文的json文件出现乱码的情况。