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 ?
Forum Updated to NodeBB v4.3 + New Features

QJsonArray.toArray() then to QStringList ?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 4.5k Views 1 Watching
  • 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.
  • S Offline
    S Offline
    SPlatten
    wrote on 7 May 2021, 14:10 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

    J 1 Reply Last reply 7 May 2021, 14:26
    0
    • S SPlatten
      7 May 2021, 14:10

      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?

      J Offline
      J Offline
      JonB
      wrote on 7 May 2021, 14:26 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.

      S 1 Reply Last reply 7 May 2021, 14:34
      0
      • J JonB
        7 May 2021, 14:26

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

        S Offline
        S Offline
        SPlatten
        wrote on 7 May 2021, 14:34 last edited by SPlatten 5 Jul 2021, 14:36
        #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
        • J Offline
          J Offline
          JoeCFD
          wrote on 7 May 2021, 14:40 last edited by
          #4
          This post is deleted!
          S 1 Reply Last reply 7 May 2021, 14:41
          1
          • J JoeCFD
            7 May 2021, 14:40

            This post is deleted!

            S Offline
            S Offline
            SPlatten
            wrote on 7 May 2021, 14:41 last edited by
            #5

            @JoeCFD , thank you, I just put together:

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

            Kind Regards,
            Sy

            J 1 Reply Last reply 7 May 2021, 14:43
            0
            • S SPlatten
              7 May 2021, 14:41

              @JoeCFD , thank you, I just put together:

              foreach( QVariant vItem, vlstOptions ) {
                  slstOptions << vItem.toString();
              }
              
              J Offline
              J Offline
              JoeCFD
              wrote on 7 May 2021, 14:43 last edited by JoeCFD 5 Jul 2021, 14:46
              #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.

              S 1 Reply Last reply 7 May 2021, 15:30
              0
              • J JoeCFD
                7 May 2021, 14:43

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

                S Offline
                S Offline
                SPlatten
                wrote on 7 May 2021, 15:30 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

                1/7

                7 May 2021, 14:10

                • Login

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