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. How to process json array with QJsonxxx?
Forum Updated to NodeBB v4.3 + New Features

How to process json array with QJsonxxx?

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

    Assuming that jsonStr is a QString variable whose value is [{"groupId":"121","description":"0213test1","name":"0213test1"}, {"groupId":"122","description":"0213test2","name":"0213test2"}].

    How can I process it with QJsonxxx class as jsonStr being its input?

    I have a workaround as the following code snippet shows:

    //_groupServiceInfoMap is a map of QMap<int, GroupServiceInfo>;
    // the definition  of GroupServiceInfo  is omitted here;
    QString jsonStr2 = jsonStr.mid(1, jsonStr.length() - 2);
    
    	foreach (QString item, jsonStr2.split("},{"))
    	{
    		GroupServiceInfo info;
    
    		item.remove("{");
    		item.remove("}");
    		item = "{" + item + "}";
    		QJsonParseError json_error;
    		QJsonDocument doucment = QJsonDocument::fromJson(item.toUtf8(), &json_error);
    		if (json_error.error == QJsonParseError::NoError)
    		{
    			if (!(doucment.isNull() || doucment.isEmpty()))
    			{
    				QVariantMap m = doucment.toVariant().toMap();
    				if (m.contains("groupId")) {
    					info.id = m["groupId"].toInt();
    				}
    				if (m.contains("description")) {
    					info.desc = m["description"].toString();
    				}
    				if (m.contains("name")) {
    					info.name = m["name"].toString();
    				}
    				_groupServiceInfoMap.insert(info.id, info);
    			}
    		}
    	}
    
    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Here's an example of how to handle a JSON array https://github.com/sierdzio/stimeline/blob/master/src/stagsmodel.cpp#L42

      (Z(:^

      1 Reply Last reply
      4
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        Maybe I'm being stupid, can't you just use QJsonArray?

        const QString jsonString(R"**([{"groupId":"121","description":"0213test1","name":"0213test1"}, {"groupId":"122","description":"0213test2","name":"0213test2"}])**");
        const QJsonDocument doucment = QJsonDocument::fromJson(jsonString);
        Q_ASSERT(doucment.isArray());
        const QJsonArray documentArray = doucment.array();
        for(const QJsonValue& jVal : documentArray){
        qDebug() << jVal["groupId"].toInt() << jVal["description"].toString() << jVal["name"].toString();
        }
        

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        D 1 Reply Last reply
        5
        • D Offline
          D Offline
          dscyw
          wrote on last edited by dscyw
          #4

          @sierdzio Thx. My final solution is as follow:

           //_groupServiceInfoMap is a map of QMap<int, GroupServiceInfo>;
                  // the definition  of GroupServiceInfo  is omitted here;
                  const QJsonDocument doucment = QJsonDocument::fromJson(jsonStr.toUtf8());
          	Q_ASSERT(doucment.isArray());
          	const QJsonArray documentArray = doucment.array();
          	GroupServiceInfo info;
          	for (const QJsonValue &i : documentArray) {
          		const QJsonObject obj(i.toObject());
          		QVariantMap m = obj.toVariantMap();
          		if (m.contains("groupId")) {
          			info.id = m["groupId"].toInt();
          		}
          		if (m.contains("description")) {
          			info.desc = m["description"].toString();
          		}
          		if (m.contains("name")) {
          			info.name = m["name"].toString();
          		}
          		_groupServiceInfoMap.insert(info.id, info);
          	}
          
          1 Reply Last reply
          2
          • VRoninV VRonin

            Maybe I'm being stupid, can't you just use QJsonArray?

            const QString jsonString(R"**([{"groupId":"121","description":"0213test1","name":"0213test1"}, {"groupId":"122","description":"0213test2","name":"0213test2"}])**");
            const QJsonDocument doucment = QJsonDocument::fromJson(jsonString);
            Q_ASSERT(doucment.isArray());
            const QJsonArray documentArray = doucment.array();
            for(const QJsonValue& jVal : documentArray){
            qDebug() << jVal["groupId"].toInt() << jVal["description"].toString() << jVal["name"].toString();
            }
            
            D Offline
            D Offline
            dscyw
            wrote on last edited by
            #5

            jVal["groupId"] doesn't compile.

            @VRonin said in How to process json array with QJsonxxx?:

            Maybe I'm being stupid, can't you just use QJsonArray?

            const QString jsonString(R"**([{"groupId":"121","description":"0213test1","name":"0213test1"}, {"groupId":"122","description":"0213test2","name":"0213test2"}])**");
            const QJsonDocument doucment = QJsonDocument::fromJson(jsonString);
            Q_ASSERT(doucment.isArray());
            const QJsonArray documentArray = doucment.array();
            for(const QJsonValue& jVal : documentArray){
            qDebug() << jVal["groupId"].toInt() << jVal["description"].toString() << jVal["name"].toString();
            }
            
            VRoninV 1 Reply Last reply
            2
            • D dscyw

              jVal["groupId"] doesn't compile.

              @VRonin said in How to process json array with QJsonxxx?:

              Maybe I'm being stupid, can't you just use QJsonArray?

              const QString jsonString(R"**([{"groupId":"121","description":"0213test1","name":"0213test1"}, {"groupId":"122","description":"0213test2","name":"0213test2"}])**");
              const QJsonDocument doucment = QJsonDocument::fromJson(jsonString);
              Q_ASSERT(doucment.isArray());
              const QJsonArray documentArray = doucment.array();
              for(const QJsonValue& jVal : documentArray){
              qDebug() << jVal["groupId"].toInt() << jVal["description"].toString() << jVal["name"].toString();
              }
              
              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              @dscyw said in How to process json array with QJsonxxx?:

              jVal["groupId"] doesn't compile.

              Correct, sorry, it's jVal.toJsonObject().value("groupId")

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              0

              • Login

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