Skip to content
  • 0 Votes
    10 Posts
    351 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
    14 Posts
    5k Views
    H

    I did resolve it using rapidjson instead of QJsonObject
    Thank you all

  • 0 Votes
    3 Posts
    293 Views
    M

    @Christian-Ehrlicher Ok, thank you so much :)

  • 0 Votes
    10 Posts
    5k Views
    mrjjM

    @Christian-Ehrlicher
    ahh, you are right. Good catch.
    http://doc.qt.io/qt-5/qjsonvalue.html#toDouble
    converts the QJsonValue to double if its that type().
    in this case it will return defaultValue (0)

    so its something like

    QJsonValue bid1 = jsonObj.value("bid_1"); QString asStr=bid1.toString(); double n = asStr.toDouble(); qDebug() << n;
  • 0 Votes
    5 Posts
    5k Views
    tansgumusT

    Thank you guys.

    What a wonderful quick responses.

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

  • 0 Votes
    2 Posts
    5k Views
    ?

    Hi @LuisAbreu!

    I cannot confirm this. Are you sure your json file is valid? The following works for me:

    ============================================
    Json file:

    { "className": "ShoppingCart", "closedOn": "Fri Mar 27 15:52:13 2015", "coupon_count": "0", "createdOn": "Fri Mar 27 15:51:09 2015", "list": [ { "_className": "Product", "discount": "0", "fromyoubeep": "1", "has_alarm": 0, "id": "5601151333457", "intendedQuantity": 0, "isReady": "1", "isUnknown": "0", "loyaltyCredit": -1, "min_age": 0, "name": "Sumo Compal Cl<C3><A1>ssico Tutti-Fruti 1lt", "userCreated": "0", "weight": "0" }, { "_className": "Product", "fromyoubeep": "0", "has_alarm": 0, "id": "", "intendedQuantity": 0, "isReady": "1", "isUnknown": "0", "userCreated": "0", "weight": "0" } ], "loyaltyCard": { "_className": "LoyaltyCard", "barcode": "2446037038353", "barcodeType": "EAN13", "discount": "0", "fromyoubeep": "1", "has_alarm": 0, "id": "2446037038353", "intendedQuantity": 0, "isReady": "1", "isUnknown": "0", "loyaltyCredit": 0, "min_age": 0, "name": "Cart<C3><A3>o Poupa Mais", "price": "0", "product_id": "-1", "quantity": "0", "quantityValidated": "0", "state": "0", "type": 1, "userCreated": "0", "weight": "0" }, "mobile_checkout": "1" }

    ============================================
    C++ code:

    QFile file("/home/pw/file.json"); if (!file.open(QIODevice::ReadOnly)) { qDebug() << "file error"; return; } const QByteArray ba = file.readAll(); file.close(); QJsonParseError err; QJsonDocument doc = QJsonDocument::fromJson(ba, &err); qDebug() << err.errorString(); qDebug() << err.offset; QJsonObject sett2 = doc.object(); qDebug() << sett2.isEmpty(); QJsonObject sett3 = sett2.value(QString("loyaltyCard")).toObject(); qDebug() << sett3.isEmpty(); QJsonValue sett4 = sett3.value(QString("barcode")); qDebug() << sett4.toString();

    ============================================
    Cheers!