【发布时间】:2017-02-28 07:56:30
【问题描述】:
我想使用QNetworkAccessManager::post 方法发送 POST 请求,但是我的 JSON 有一些国际字符,例如 ąę。
不幸的是,我的 WebAPI 在从QByteArray 格式解码此字符串时遇到了一些问题:"\xC4\x99\xC4\x85"。
使用curl,我的 API 在未转义字符串和 Unicode 转义字符串的情况下工作正常:\u0119\u0105。
我尝试了this,但在我将QString 转换为unicode 转义后QString:
QString toUnicodeEscaped(const QString& str){
QString escaped;
escaped.reserve(6 * str.size());
for (QString::const_iterator it = str.begin(); it != str.end(); ++it) {
QChar ch = *it;
ushort code = ch.unicode();
if (code < 0x80) {
escaped += ch;
} else {
escaped += "\\u";
escaped += QString::number(code, 16).rightJustified(4, '0');
}
}
return escaped;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QString s = "ęą";
qDebug() << "s: " << toUnicodeEscaped(s);
}
我得到带有两个反斜杠的字符串:
s: "\\u0119\\u0105"
我尝试用一个反斜杠替换两个反斜杠,但没有成功:
qDebug() << "s: " << toUnicodeEscaped(s).replace("\\\\","\\");
如何替换它们?
【问题讨论】:
标签: qt unicode utf-8 qnetworkaccessmanager qbytearray