Question

I am trying to write a C++ Qt 4.7 app that gets some JSON from a web API. I have done some reading and JsonCpp seems to be the best. So I built it just find and added it to my project just fine.

void RetrievingInformationPage::replyFinished(QNetworkReply *reply)
{

    Json::Value root;
    Json::Reader reader;

    bool success = reader.parse(reply->readAll().data(), root);

    // here be issues
    qDebug() << QString::fromStdString(root["data"][30]["name"].toStyledString());

    return;
}

When I run this code, it outputs the name I am testing (it is a name with unicode in it) but the special characters output as complete gibberish ("à¤?à¥à¤²à¤¿à¤«à¤°à¥à¤¡"). The unicode went in as a JSON string "\u0915\u094d\u0932\u093f\u092b\u0930\u094d\u0921", then I assume JsonCpp converts it into the actual unicode characters. I was hoping that QString::fromStdString would take unicode in a std::string and store it in a QString, but clearly it is messing up somewhere.

What am I missing?

Was it helpful?

Solution

As far as I can tell from a short look at the JsonCpp documentation, the library delivers strings in UTF-8 encoding. To convert to QStrings use QString::fromUTF8 for qDebug use qPrintable

QString::fromStdString uses QString::fromAscii, see the documentation here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top