使用 QNetworkReply* m_pNetworkReply; //封装请求返回信息
m_pNetworkReply获取网络得到的数据
QByteArray resultContent = m_pNetworkReply->readAll();
// 假如 resultContent为字符串 "张三78zn"
//则 resultContent的长度为 2*3+2+2 =10,即汉字占用了三个字节!
QTextCodec* pCodec = QTextCodec::codecForName("UTF-8");
QString strResult = pCodec->toUnicode(resultContent);
//经过处理之后 strResult 的长度为 6个字节
//此时用 QMessageBox 打印可以完整的看到 张三78zn
若调用
Json::Reader reader;
Json::Value value;
if (reader.parse(strResult.toStdString(), value))
{
// printf("Started Parse \n\n\n");
// printf("value asString() %s \n\n\n, ",value["state"].asString().data());
// std::string outValue=value["color"].asString();
ui->EditColor->setText(QString::fromUtf8(value["color"].asString().c_str() ) ); //产品颜色
ui->EditName->setText(QString::fromUtf8(value["materialName"].asString().c_str() ) );//产品原料
ui->EditCheck->setText(QString::fromUtf8(value["finish_name"].asString().c_str() ) );//操作人员
ui->EditOperate->setText(QString::fromUtf8(value["stateName"].asString().c_str()) );//最近操作
ui->ModuleName->setText(QString::fromUtf8(value["mouldName"].asString().c_str()) );//模具名称
}
则会出问题,汉字无法显示!!原因是 Json::Reader 解析时,汉字需要占用3个字节 进行解析
正确的做法是
此处不要强制转换
QTextCodec* pCodec = QTextCodec::codecForName("UTF-8");
QString strResult = pCodec->toUnicode(resultContent);
替换为QString strResult(resultContent); 即可
m_pNetworkReply获取网络得到的数据
QByteArray resultContent = m_pNetworkReply->readAll();
// 假如 resultContent为字符串 "张三78zn"
//则 resultContent的长度为 2*3+2+2 =10,即汉字占用了三个字节!
QTextCodec* pCodec = QTextCodec::codecForName("UTF-8");
QString strResult = pCodec->toUnicode(resultContent);
//经过处理之后 strResult 的长度为 6个字节
//此时用 QMessageBox 打印可以完整的看到 张三78zn
若调用
Json::Reader reader;
Json::Value value;
if (reader.parse(strResult.toStdString(), value))
{
// printf("Started Parse \n\n\n");
// printf("value asString() %s \n\n\n, ",value["state"].asString().data());
// std::string outValue=value["color"].asString();
ui->EditColor->setText(QString::fromUtf8(value["color"].asString().c_str() ) ); //产品颜色
ui->EditName->setText(QString::fromUtf8(value["materialName"].asString().c_str() ) );//产品原料
ui->EditCheck->setText(QString::fromUtf8(value["finish_name"].asString().c_str() ) );//操作人员
ui->EditOperate->setText(QString::fromUtf8(value["stateName"].asString().c_str()) );//最近操作
ui->ModuleName->setText(QString::fromUtf8(value["mouldName"].asString().c_str()) );//模具名称
}
则会出问题,汉字无法显示!!原因是 Json::Reader 解析时,汉字需要占用3个字节 进行解析
正确的做法是
此处不要强制转换
QTextCodec* pCodec = QTextCodec::codecForName("UTF-8");
QString strResult = pCodec->toUnicode(resultContent);
替换为QString strResult(resultContent); 即可