Skip to content
  • 0 Votes
    10 Posts
    349 Views
    JonBJ

    @SimonSchroeder said in QByteArray to QVariantMap while conserving numerical type:

    In that case you need to specify some formatting to make sure the number is written out in a way that makes it clear it is floating point.

    I don't follow you here. How would any kind of formatting of the number at sender side make any difference to the result received at the receiver side using a JSON parser to read it which will return a "numeric"/QJsonValue::Type::Double? Other than sending it is a string type and doing the numeric conversion at receiver side, which is quite different.

  • 0 Votes
    5 Posts
    314 Views
    Paul ColbyP

    HI @R-P-H ,

    Besides converting the QByteArray to a QString, how would I extract the value if I added {} to the QByteArray ...

    As long as the QByteArray contains a valid JSON value, you can either wrap it in [ and ] to make it a JSON array, or { "key": and } to make it a JSON object. If wrapping in an array, you would then fetch the first array item to get the original JSON value. Of if wrapping in an object, fetch the value corresponding to the key you used in the wrapper.

    Here's a complete example:

    const QByteArray json{ "\"My Value\" "}; qDebug().noquote() << "input " << json; qDebug().noquote() << "parse-as-is " << QJsonDocument::fromJson(json); // Will fail. const QJsonDocument arrayDoc = QJsonDocument::fromJson("[" + json + "]"); qDebug().noquote() << "wrapped-in-array " << arrayDoc; qDebug().noquote() << "unwrapped-array " << arrayDoc.array().at(0); const QJsonDocument objectDoc = QJsonDocument::fromJson("{ \"value\": " + json + "}"); qDebug().noquote() << "wrapped-in-object " << objectDoc; qDebug().noquote() << "unwrapped-object " << objectDoc.object().value(QStringLiteral("value"));

    Which outputs:

    input "My Value" parse-as-is QJsonDocument() wrapped-in-array QJsonDocument(["My Value"]) unwrapped-array QJsonValue(string, My Value) wrapped-in-object QJsonDocument({"value":"My Value"}) unwrapped-object QJsonValue(string, My Value)

    Cheers.

  • 0 Votes
    8 Posts
    5k Views
    D

    Hi @SGaist , no sorry. This was my first try to solve this. It dosn't work because of:

    @JNBarchan said:

    Once JSON has taken it as a double, it's too late.

    With a small test program I testes Qt-JSON: https://github.com/qt-json/qt-json

    #include <QCoreApplication> #include <QDebug> #include "json.h" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QString json = "{\"success\":true,\"message\":\"some-message\",\"number1\":1.234,\"number2\":\"5.678\"}"; bool ok; QtJson::JsonObject result = QtJson::parse(json, ok).toMap(); qDebug() << "number1: " << result["number1"].toString(); qDebug() << "number2: " << result["number2"].toString(); return a.exec(); }

    Which gives me the following output:

    number1: "1.234" number2: "5.678"

    So I think I can work with this library, but a bit sad about the truth, that such a case can't be handled without external libraries.

  • 0 Votes
    9 Posts
    7k Views
    Q

    @JKSH

    Thank you! That did clear my mind! :D

    QJsonDocument to array, then foreach, next object value "site" toObject and now tadaaa object.value("name").toString;

    Great, thanks again!

  • convert QJsonValue to Int

    Solved General and Desktop
    4
    0 Votes
    4 Posts
    6k Views
    the_T

    @SGaist, @JKSH

    Thanks for clarification. As i get this JSON from a webservice (not coded by me) i will have to use the "hard" way of JSON Value -> String -> Int to get an integer value