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
    7 Posts
    1k Views
    N

    @mrjj said in How I can create Json Format in Qt ?:

    someObj

    Thanks, it helps a lot.

  • 2 Votes
    17 Posts
    10k Views
    Per Gunnar HolmP

    Thanks @kshegunov !

    The c_str() was just something we tested along the way!
    The original code was

    // Create a Json document from text. Fails for foreign characters! QJsonDocument doc = QJsonDocument::fromJson(msg.toUtf8(), &error);

    However, all the variations we/I have tried display the same problem (on CentOS 6.5, as we have discovered).

  • 0 Votes
    3 Posts
    33k Views
    4

    @Paul-Colby Thank you

  • 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!

  • 0 Votes
    12 Posts
    10k Views
    mrjjM

    @returnx
    Ok. super. please mark as solved :)

    Final note:
    When you deploy,
    there might be more reads for full json string.
    The code you shown, will try to parse on each read. Make sure the code can
    handle that it comes in blocks and not bail out if parse fails.