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. QJsonArray.toArray() then to QStringList ?
QtWS25 Last Chance

QJsonArray.toArray() then to QStringList ?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 4.4k 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by
    #1

    I have a JSON array in a MariaDB record, I want to convert this to a QStringList, so far:

        QJsonObject::iterator itOption = objJSON.find(scszOption);
        ... 
        QStringList slstOptions = itOption.value().toArray().toVariantList();
    

    How do I convert from QVariantList to an instance of QStringList?

    Kind Regards,
    Sy

    JonBJ 1 Reply Last reply
    0
    • SPlattenS SPlatten

      I have a JSON array in a MariaDB record, I want to convert this to a QStringList, so far:

          QJsonObject::iterator itOption = objJSON.find(scszOption);
          ... 
          QStringList slstOptions = itOption.value().toArray().toVariantList();
      

      How do I convert from QVariantList to an instance of QStringList?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @SPlatten
      QVariantList is a QList<QVariant>. Iterate the QVariant elements. There is QVariant::toString(), or QVariant::toStringList() if each individual variant is itself a list.

      SPlattenS 1 Reply Last reply
      0
      • JonBJ JonB

        @SPlatten
        QVariantList is a QList<QVariant>. Iterate the QVariant elements. There is QVariant::toString(), or QVariant::toStringList() if each individual variant is itself a list.

        SPlattenS Offline
        SPlattenS Offline
        SPlatten
        wrote on last edited by SPlatten
        #3

        @JonB said in QJsonArray.toArray() then to QStringList ?:

        QVariant::toStringList()

        That doesn't appear to be available in Qt 5.9.2, only toStdList. However...I think I've got it:

        QVariantList vlstOptions = itOption.value().toArray().toVariantList();
        QStringList slstOptions = vlstOptions.value_type.toStringList();
        

        Kind Regards,
        Sy

        1 Reply Last reply
        0
        • JoeCFDJ Offline
          JoeCFDJ Offline
          JoeCFD
          wrote on last edited by
          #4
          This post is deleted!
          SPlattenS 1 Reply Last reply
          1
          • JoeCFDJ JoeCFD

            This post is deleted!

            SPlattenS Offline
            SPlattenS Offline
            SPlatten
            wrote on last edited by
            #5

            @JoeCFD , thank you, I just put together:

            foreach( QVariant vItem, vlstOptions ) {
                slstOptions << vItem.toString();
            }
            

            Kind Regards,
            Sy

            JoeCFDJ 1 Reply Last reply
            0
            • SPlattenS SPlatten

              @JoeCFD , thank you, I just put together:

              foreach( QVariant vItem, vlstOptions ) {
                  slstOptions << vItem.toString();
              }
              
              JoeCFDJ Offline
              JoeCFDJ Offline
              JoeCFD
              wrote on last edited by JoeCFD
              #6

              @SPlatten for() is better than foreach and you may need to check if qvariant is valid. <== no check is needed. If it is invalid, toString() returns empty string.

              SPlattenS 1 Reply Last reply
              0
              • JoeCFDJ JoeCFD

                @SPlatten for() is better than foreach and you may need to check if qvariant is valid. <== no check is needed. If it is invalid, toString() returns empty string.

                SPlattenS Offline
                SPlattenS Offline
                SPlatten
                wrote on last edited by
                #7

                @JoeCFD , in the end I found a better solution:

                QString strOption(itOption.value().toString());
                QJsonDocument docJSON(QJsonDocument::fromJson(strOption.toLatin1()));
                QStringList slstOptions(docJSON.toVariant().toStringList());
                

                Kind Regards,
                Sy

                1 Reply Last reply
                2

                • Login

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