引用头文件
| #include <QJsonObject> |
| #include <QJsonDocument> |
| #include <QDebug> |
复制
Json转字符串
| |
| QJsonObject json; |
| json.insert("state", "error"); |
| json.insert("msg", "asdfghjkl"); |
| |
| |
| QJsonDocument documentStr; |
| documentStr.setObject(json); |
| #if 0 |
| QByteArray byteArray = documentStr.toJson(QJsonDocument::Compact); |
| #else |
| QByteArray byteArray = documentStr.toJson(QJsonDocument::Indented); |
| #endif |
| QString strJson(byteArray); |
| qDebug() << strJson; |
复制
字符串转Json
| QString str = "{\n \"msg\": \"asdfghjkl\",\n \"state\": \"error\"\n}\n"; |
| |
| |
| QJsonParseError jsonError; |
| QJsonDocument documentJson = QJsonDocument::fromJson(str.toStdString().data(), &jsonError); |
| if (jsonError.error == QJsonParseError::NoError) |
| { |
| QJsonObject json = documentJson.object(); |
| QString msg = json["msg"].toString(); |
| QString state = json["state"].toString(); |
| qDebug() << msg << state; |
| } |
| |
| QString lstStr = "[{\"msg\": \"hello\",\"name\": \"淘气小狼人\",\"age\": 18},{\"msg\": \"hello\",\"name\": \"兄弟们\",\"age\": 18}]"; |
| QJsonParseError jsonError; |
| QJsonDocument lstStrJson = QJsonDocument::fromJson(lstStr.toUtf8(), &jsonError); |
| if(jsonError.error == QJsonParseError::NoError) |
| { |
| QVariantList lstMSG = lstStrJson.array().toVariantList(); |
| for (int i =0;i<lstMSG.count();i++) { |
| QVariantMap strMap = lstMSG.at(i).toMap(); |
| QString sMSG = strMap.value("msg").toString(); |
| QString sNAME = strMap.value("name").toString(); |
| int AGE = strMap.value("age").toInt(); |
| qDebug() << sMSG <<sNAME <<AGE; |
| } |
| } |
| |
复制
下列还有各种形式的转换供大家参考,欢迎补充!!!
| QString MainWindow::func1(QJsonObject json) |
| { |
| |
| |
| |
| |
| json.insert("userName", QString::fromLocal8Bit("淘气小狼人")); |
| json.insert("password", "123456"); |
| json.insert("flag",QJsonValue(true)); |
| json.remove("flag"); |
| qDebug() << json.size(); |
| |
| |
| |
| QJsonDocument document; |
| |
| document.setObject(json); |
| |
| #if 0 |
| QByteArray byteArray = document.toJson(QJsonDocument::Compact); |
| #else |
| QByteArray byteArray = document.toBinaryData(); |
| #endif |
| QString strJson(byteArray); |
| |
| |
| QJsonParseError jsonParseError; |
| |
| QJsonObject ret = QJsonDocument::fromBinaryData(byteArray, QJsonDocument::Validate).object(); |
| QJsonObject re1t = QJsonDocument::fromJson(byteArray, &jsonParseError).object(); |
| if (jsonParseError.error == QJsonParseError::NoError) qDebug() << "success"; |
| |
| |
| QJsonArray ret1 = QJsonDocument::fromBinaryData( byteArray, QJsonDocument::BypassValidation).array(); |
| QJsonArray ret2 = QJsonDocument::fromJson( byteArray, &jsonParseError).array(); |
| if (jsonParseError.error == QJsonParseError::NoError) qDebug() << "success"; |
| |
| |
| return strJson; |
| } |
| |
| QJsonObject MainWindow::func2(QString jsonString) |
| { |
| QJsonDocument jsonDocument; |
| QJsonObject jsonObject; |
| |
| #if 0 |
| QJsonParseError jsonError; |
| QJsonDocument jsonDocument = QJsonDocument::fromJson(jsonString.toLocal8Bit().data(),&jsonError); |
| if (jsonError.error == QJsonParseError::NoError) |
| jsonObject = jsonDocument.object(); |
| #else |
| |
| jsonDocument = QJsonDocument::fromJson(jsonString.toLocal8Bit().data()); |
| |
| if(jsonDocument.isNull()) |
| qDebug()<< "String NULL"<< jsonString.toLocal8Bit().data(); |
| #endif |
| |
| jsonObject = jsonDocument.object(); |
| |
| return jsonObject; |
| } |
| |
| QString MainWindow::func3(QJsonObject jsonObject) |
| { |
| |
| return QString(QJsonDocument(jsonObject).toJson()); |
| } |
| |
| QString MainWindow::func4(QVariantMap mapInfo) |
| { |
| mapInfo["userName"] = "淘气小狼人"; |
| mapInfo.insert("password","123456"); |
| QJsonDocument json = QJsonDocument::fromVariant(mapInfo); |
| #if 0 |
| QString str = json.toJson(QJsonDocument::Indented); |
| #else |
| QString str = json.toJson(QJsonDocument::Compact); |
| #endif |
| return str; |
| } |
| |
| QString MainWindow::func5(QString str) |
| { |
| str = "{\n \"userName\": \"淘气小狼人\"\n \"password\": \"123456\",\n}\n"; |
| QJsonDocument doc = QJsonDocument::fromJson(str.toUtf8()); |
| #if 0 |
| QJsonArray jsonArry = doc.array(); |
| QJsonDocument jsonDoc(jsonArry); |
| str = QString(jsonDoc.toJson(QJsonDocument::Compact)); |
| #else |
| QJsonObject jsonObj = doc.object(); |
| QJsonDocument jsonDoc(jsonObj); |
| str = QString(jsonDoc.toJson(QJsonDocument::Indented)); |
| #endif |
| return str; |
| } |
| |
复制