QJson解析数据基本用法

#include 
#include 
#include 
#include 

  ...

    QString str = "{\"errno\":0,\"errmsg\":\"\",\"data\":{\"appid\":\"134304921\",\"rid\":-58474238,\"sign\":\"a9a6dee9633e76c3e21253cbd65ea361\",\"authType\":\"3\",\"ts\":1477290743000,\"chat_addr_list\":[\"54.222.202.135:443\",\"54.222.183.38:443\"]}}";

    QJsonParseError jsonError;
    QJsonDocument jsonDoc = QJsonDocument::fromJson(str.toLatin1(), &jsonError);

    if(jsonError.error == QJsonParseError::NoError)
    {
        if(jsonDoc.isObject())
        {
            QJsonObject obj = jsonDoc.object();

            if(obj.contains("data"))
            {
                QJsonValue value= obj.take("data");
                if(value.isObject())
                {
                    qDebug()<< "data is object";
                    QJsonObject dataObj = value.toObject();

                    qDebug() << QString(QJsonDocument(dataObj).toJson());

                    if(dataObj.contains("appid"))
                    {
                        dataObj.take("appid").toString();
                    }
                    if(dataObj.contains("chat_addr_list"))
                    {
                        QJsonValue value_list = dataObj.take("chat_addr_list");
                        if(value_list.isArray())
                        {
                            QJsonArray arr = value_list.toArray();
                            qDebug() << arr.size();
                            for(int i = 0; i < arr.size(); i++)
                            {
                                QJsonValue value = arr.at(i);
                                if(value.isString())
                                    qDebug() << value.toString();
                            }
                        }
                    }
                }
            }
//            if(obj.contains("errno"))
        }
    }

你可能感兴趣的:(QT,C++)