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. [SOLVED]parse Json in Qt5
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]parse Json in Qt5

Scheduled Pinned Locked Moved General and Desktop
jsonjson parser
7 Posts 2 Posters 29.2k Views 2 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.
  • 4 Offline
    4 Offline
    4j1th
    wrote on last edited by 4j1th
    #1

    I want parse a json and add it into sqlite, this json file parse from php

    test.json
    [{"name":"ugiuiug","dob":"0000-00-00"},{"name":"jghighui","dob":"0000-00-00"},{"name":"igiyug","dob":"0000-00-00"},{"name":"jhu","dob":"0000-00-00"}]

    qt code

        QFile file;
        file.setFileName("test1.json");
        file.open(QIODevice::ReadOnly | QIODevice::Text);
        val = file.readAll();
        file.close();
        QJsonDocument doc = QJsonDocument::fromJson(val.toUtf8());
    
        //get the jsonObject
        QJsonObject jObject = doc.object();
    
        //convert the json object to variantmap
        QVariantMap mainMap = jObject.toVariantMap();
    
        //convert the json object to variantmap
        QVariantMap dataMap = mainMap['0'].toMap();
    
        qDebug() << dataMap['name'].toString();
    

    but an error produced : invalid user-defined conversion from 'char' to 'const QString&' [-fpermissive]
    QVariantMap dataMap = mainMap['1'].toMap();
    ^

    Pardon my English
    Thank you.

    yeckelY 1 Reply Last reply
    0
    • 4 4j1th

      I want parse a json and add it into sqlite, this json file parse from php

      test.json
      [{"name":"ugiuiug","dob":"0000-00-00"},{"name":"jghighui","dob":"0000-00-00"},{"name":"igiyug","dob":"0000-00-00"},{"name":"jhu","dob":"0000-00-00"}]

      qt code

          QFile file;
          file.setFileName("test1.json");
          file.open(QIODevice::ReadOnly | QIODevice::Text);
          val = file.readAll();
          file.close();
          QJsonDocument doc = QJsonDocument::fromJson(val.toUtf8());
      
          //get the jsonObject
          QJsonObject jObject = doc.object();
      
          //convert the json object to variantmap
          QVariantMap mainMap = jObject.toVariantMap();
      
          //convert the json object to variantmap
          QVariantMap dataMap = mainMap['0'].toMap();
      
          qDebug() << dataMap['name'].toString();
      

      but an error produced : invalid user-defined conversion from 'char' to 'const QString&' [-fpermissive]
      QVariantMap dataMap = mainMap['1'].toMap();
      ^

      yeckelY Offline
      yeckelY Offline
      yeckel
      wrote on last edited by
      #2

      @4j1th mainMap['1'].toMap(); '1' is character, you would like to use 1 without apostrophes, or "0" (quotation marks) when it's a string. In your case put there just the number: mainMap[1].toMap();

      4 1 Reply Last reply
      0
      • yeckelY yeckel

        @4j1th mainMap['1'].toMap(); '1' is character, you would like to use 1 without apostrophes, or "0" (quotation marks) when it's a string. In your case put there just the number: mainMap[1].toMap();

        4 Offline
        4 Offline
        4j1th
        wrote on last edited by 4j1th
        #3

        @yeckel thank you

        updated code block

        QString val;
            QFile file;
            file.setFileName("test1.json");
            file.open(QIODevice::ReadOnly | QIODevice::Text);
            val = file.readAll();
            file.close();
            QJsonDocument doc = QJsonDocument::fromJson(val.toUtf8());
        
            //get the jsonObject
            QJsonObject jObject = doc.object();
        
            //convert the json object to variantmap
            QVariantMap mainMap = jObject.toVariantMap();
        
            //convert the json object to variantmap
            QVariantMap dataMap = mainMap[1].toMap();
        
            qDebug() << dataMap["name"].toString();
        

        I tried that too but still got error
        error: invalid conversion from 'int' to 'const char*' [-fpermissive]

        Pardon my English
        Thank you.

        yeckelY 1 Reply Last reply
        0
        • 4 4j1th

          @yeckel thank you

          updated code block

          QString val;
              QFile file;
              file.setFileName("test1.json");
              file.open(QIODevice::ReadOnly | QIODevice::Text);
              val = file.readAll();
              file.close();
              QJsonDocument doc = QJsonDocument::fromJson(val.toUtf8());
          
              //get the jsonObject
              QJsonObject jObject = doc.object();
          
              //convert the json object to variantmap
              QVariantMap mainMap = jObject.toVariantMap();
          
              //convert the json object to variantmap
              QVariantMap dataMap = mainMap[1].toMap();
          
              qDebug() << dataMap["name"].toString();
          

          I tried that too but still got error
          error: invalid conversion from 'int' to 'const char*' [-fpermissive]

          yeckelY Offline
          yeckelY Offline
          yeckel
          wrote on last edited by
          #4

          @4j1th Aa, sorry I thought you have the array already. mainMap is type of QVariantMap thus it needs QString as a key parameter.

          Watch out your json example is not a json. Something like: {"results": [{"name":"ugiuiug","dob":"0000-00-00"},{"name":"jghighui","dob":"0000-00-00"},{"name":"igiyug","dob":"0000-00-00"},{"name":"jhu","dob":"0000-00-00"}]} woud be.

          Then:
          QVariantMap mainMap = jObject.toVariantMap();
          QVariantList localList = mainMap["result"].toList();
          QVAriantMap map = localList[0].toMap();
          qDebug() << map["name"].toString;

          4 1 Reply Last reply
          0
          • yeckelY yeckel

            @4j1th Aa, sorry I thought you have the array already. mainMap is type of QVariantMap thus it needs QString as a key parameter.

            Watch out your json example is not a json. Something like: {"results": [{"name":"ugiuiug","dob":"0000-00-00"},{"name":"jghighui","dob":"0000-00-00"},{"name":"igiyug","dob":"0000-00-00"},{"name":"jhu","dob":"0000-00-00"}]} woud be.

            Then:
            QVariantMap mainMap = jObject.toVariantMap();
            QVariantList localList = mainMap["result"].toList();
            QVAriantMap map = localList[0].toMap();
            qDebug() << map["name"].toString;

            4 Offline
            4 Offline
            4j1th
            wrote on last edited by 4j1th
            #5

            @yeckel
            json file
            {"results":[{"name":"ugiuiug","dob":"0000-00-00"},{"name":"jghighui","dob":"0000-00-00"},{"name":"igiyug","dob":"0000-00-00"},{"name":"jhu","dob":"0000-00-00"}]}

            qt code

            
                //get the jsonObject
                QJsonObject jObject = doc.object();
            
                QVariantMap mainMap = jObject.toVariantMap();
                QVariantList localList = mainMap["result"].toList();    
                QVariantMap map = localList[1].toMap();
            
                qDebug() << map["name"].toString();
            

            compiled but ASSERT failure in QList<T>::operator[]: "index out of range", file ../../../../Qt5.4.1/5.4/gcc_64/include/QtCore/qlist.h, line 486
            The program has unexpectedly finished.

            Pardon my English
            Thank you.

            1 Reply Last reply
            0
            • yeckelY Offline
              yeckelY Offline
              yeckel
              wrote on last edited by
              #6

              @4j1th
              QJsonParseError jsonError;
              QJsonDocument flowerJson = QJsonDocument::fromJson(file.readAll(),&jsonError);
              if (jsonError.error != QJsonParseError::NoError){
              qDebug() << jsonError.errorString();
              }
              QList<QVariant> list = flowerJson.toVariant().toList();
              QMap<QString, QVariant> map = list[0].toMap();
              qDebug() << map["name"].toString();

              Has to work.

              4 1 Reply Last reply
              1
              • yeckelY yeckel

                @4j1th
                QJsonParseError jsonError;
                QJsonDocument flowerJson = QJsonDocument::fromJson(file.readAll(),&jsonError);
                if (jsonError.error != QJsonParseError::NoError){
                qDebug() << jsonError.errorString();
                }
                QList<QVariant> list = flowerJson.toVariant().toList();
                QMap<QString, QVariant> map = list[0].toMap();
                qDebug() << map["name"].toString();

                Has to work.

                4 Offline
                4 Offline
                4j1th
                wrote on last edited by
                #7

                @yeckel Thank you very much it's working now

                json file

                [{"name":"ugiuiug","dob":"0000-00-00"},{"name":"jghighui","dob":"0000-00-00"},{"name":"igiyug","dob":"0000-00-00"},{"name":"jhu","dob":"0000-00-00"}]
                

                Qt code

                
                    QFile file;
                    file.setFileName("test1.json");
                    file.open(QIODevice::ReadOnly | QIODevice::Text);
                
                
                    QJsonParseError jsonError;
                    QJsonDocument flowerJson = QJsonDocument::fromJson(file.readAll(),&jsonError);
                    if (jsonError.error != QJsonParseError::NoError){
                    qDebug() << jsonError.errorString();
                    }
                    QList<QVariant> list = flowerJson.toVariant().toList();
                    QMap<QString, QVariant> map = list[0].toMap();
                    qDebug() << map["name"].toString();
                

                Pardon my English
                Thank you.

                1 Reply Last reply
                1

                • Login

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