Qt解析json文件中路径时出现的问题(‘\‘,“\\“,‘/‘)

json文件内容:

'\'

{
	"file":"D:\work\微信.exe"
}

错误类型:

QJsonParseError::IllegalUTF8String

An illegal UTF8 sequence occurred in the input

如果想得到"D:\\work\\微信.exe",如何做?

    QString str1;
    QFile file(QApplication::applicationDirPath() + "/"+str);
    if (file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        QByteArray ba = file.readAll();
        file.close();
        QString content=QString::fromUtf8(ba);
        content = content.replace("\\", "\\\\");

        QJsonParseError json_error;
        QJsonDocument jsonDoc(QJsonDocument::fromJson(content.toUtf8(), &json_error));
        if (json_error.error == QJsonParseError::NoError)
        {
            QJsonObject rootObj = jsonDoc.object();
            str1 = rootObj.value("file").toString();
            qDebug()<

 (1)QByteArray--->(2)QString--->replace--->(3)QString--->(4)QByteArray

 (2){\n\t\"file\":\"D:\\work\\微信.exe\"\n}

 (4){\n\t\"file\":\"D:\\\\work\\\\微信.exe\"\n}

关键:

        QString content=QString::fromUtf8(ba);
        content = content.replace("\\", "\\\\");
        QJsonParseError json_error;
        QJsonDocument jsonDoc(QJsonDocument::fromJson(content.toUtf8(), &json_error));

如何写是可以的?(不需要进行处理)

(1)\\

{
	"file":"D:\\work\\微信.exe"
}

(2) /

{
	"file":"D:/work/微信.exe"
}

你可能感兴趣的:(json)