zoukankan      html  css  js  c++  java
  • Qt 解释JSon数据

    jsondata={"cmd":"meetingmemberlist","memberinfos":[{"name":"李","number":"668162","status":"Offline"},{"name":"66668888","status":"Offline"},{"name":"1.221","number":"1234561","status":"Offline"},{"name":"1.42","status":"Offline"}],"session":"2989479764ll","touid":"19","xns":"18"}

    void  CEMViewMenuInteraction::get_memberdata_from_jsondate(std::string jsondata)
    {
        QByteArray ByteArr = QByteArray(jsondata.c_str());
        //判断字符串转化为 QJsonDocument  是否出现了错误
        QJsonParseError jsonError;//Qt5新类
        QJsonDocument jsonDoc = QJsonDocument::fromJson(ByteArr, &jsonError);//Qt5新类
        if (jsonError.error == QJsonParseError::NoError)
       {
    //        qDebug()<< "parse jsondata";
           if (jsonDoc.isObject())
           {
               QJsonObject rootObj = jsonDoc.object();
               QString cmd;
               QString session;
               //是否含有key  cmd
               if (rootObj.contains("cmd"))
               {
                   //取出key为rootpath的值
                   QJsonValue value = rootObj.value("cmd");
                   //判断是否是string类型
                   if (value.isString())
                   {
                       cmd = value.toString();
    //                   qDebug()<< "cmd=" <<cmd;
                   }
               }
               if (rootObj.contains("session"))
               {
                   //取出key为 session 的值
                   QJsonValue value = rootObj.value("session");
                   //判断是否是string类型
                   if (value.isString())
                   {
                       session = value.toString();
    //                   qDebug()<< "session=" << session;
                   }
               }
               if (rootObj.contains("memberinfos"))
               {
                   QJsonValue valueArray = rootObj.value("memberinfos");
                   //判断类型是否为array,并且将array遍历出来
                   if (valueArray.isArray())
                   {
                       QJsonArray jsonArray = valueArray.toArray();
                       QString child_name = "null";
                       QString child_status = "null";
                       for (int i = 0; i < jsonArray.count();i++)
                       {
                           QJsonValue childValue = jsonArray[i];
                           if (childValue.isObject())
                           {
                               QJsonObject  childObject = childValue.toObject();
                               if (childObject.contains("name"))
                               {
                                   QJsonValue valueJson = childObject.value("name");
                                   if (valueJson.isString())
                                   {
                                       child_name = valueJson.toString();
    //                                   qDebug()<< "name=" <<child_name;
                                   }
                               }
                               if (childObject.contains("status"))
                               {
                                   QJsonValue valueJson = childObject.value("status");
                                   if (valueJson.isString())
                                   {
                                       child_status = valueJson.toString();
    //                                   qDebug()<< "status=" <<child_status;
                                   }
                               }
                               m_MemberListMap[child_name] = child_status;
                           }
                       }
                   }
               }
    
           }
       }
    }
  • 相关阅读:
    数据库
    Python基础
    flask 文件的上传下载和excel操作
    Django 知识点补充
    Django部署时为什么要用 uWSGI与 Nginx? 以及 WSGI,uwsgi等协议
    Memcached 补充
    websocket基本概念
    Tornado 异步非阻塞
    Tornado 自定义session,与一致性哈希 ,基于redis 构建分布式 session框架
    functools 和 itertools
  • 原文地址:https://www.cnblogs.com/nanqiang/p/11212870.html
Copyright © 2011-2022 走看看