Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Parsing a QJsonObject from inside another QJsonObject
QtWS25 Last Chance

Parsing a QJsonObject from inside another QJsonObject

Scheduled Pinned Locked Moved General and Desktop
jsonqjsonobjectqjsonparsing jsonjson parser
2 Posts 2 Posters 4.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LuisAbreu
    wrote on 10 Apr 2015, 14:58 last edited by LuisAbreu 4 Oct 2015, 15:01
    #1

    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... :/

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on 10 Apr 2015, 18:21 last edited by A Former User 4 Oct 2015, 19:56
      #2

      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!

      1 Reply Last reply
      0

      1/2

      10 Apr 2015, 14:58

      • Login

      • Login or register to search.
      1 out of 2
      • First post
        1/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved