Parsing a QJsonObject from inside another QJsonObject



  • So I have a json that looks like this:

         "_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",
     } 
    

    as you can see inside the main json I have another json. and I need to parse it in my c++ code.
    This is the way I'm doing it, but the json i'm getting is empty:

    QJsonObject sett3 = sett2.value(QString("loyaltyCard")).toObject();

    sett2 is the main json. And im able to access all of its fields except the ones with a json inside, for example, sett3 is returning empty... :/


  • Moderators

    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!



Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.