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?

How to process json array with QJsonxxx?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 1.6k 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.
  • D Offline
    D Offline
    dscyw
    wrote on 28 Feb 2018, 03:01 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
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 28 Feb 2018, 06:43 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
      • V Offline
        V Offline
        VRonin
        wrote on 28 Feb 2018, 08:42 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 28 Feb 2018, 10:19
        5
        • D Offline
          D Offline
          dscyw
          wrote on 28 Feb 2018, 09:32 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
          • V VRonin
            28 Feb 2018, 08:42

            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 28 Feb 2018, 10:19 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();
            }
            
            V 1 Reply Last reply 28 Feb 2018, 11:13
            2
            • D dscyw
              28 Feb 2018, 10:19

              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();
              }
              
              V Offline
              V Offline
              VRonin
              wrote on 28 Feb 2018, 11:13 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

              1/6

              28 Feb 2018, 03:01

              • Login

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