首页 前端知识 qt JSON和字符串相互转换

qt JSON和字符串相互转换

2024-05-05 12:05:04 前端知识 前端哥 115 219 我要收藏

引用头文件

#include <QJsonObject>
#include <QJsonDocument>
#include <QDebug>
复制

Json转字符串

//构建JSON对象
QJsonObject json;
json.insert("state", "error");
json.insert("msg", "asdfghjkl");
//构建JSON文档
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";
/*转为Json对象*/
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)
{
// QJsonObject类用于封装JSON对象。JSON对象是包含键值对的链表,其中键是唯一的字符串。
// 构建 JSON 对象
// Qt里存JSON字符串, 是用QByteArray来存储的,涉及到中文, 将QByteArray转换为QString
json.insert("userName", QString::fromLocal8Bit("淘气小狼人")); // insert()插入pair
json.insert("password", "123456"); // insert()插入pair
json.insert("flag",QJsonValue(true)); // 值为QJsonValue对象
json.remove("flag"); // remove()删除pair
qDebug() << json.size(); // 键值对的数目
// QJsonDocument提供了读写Json文档的方法.
// 构建 JSON 文档
QJsonDocument document;
// QJsonDocument和QJsonObject的关联通过调用QJsonDocument的setObject或者直接构造函数参数。QJsonDocument调用object函数得到 QJsonObject对象。
document.setObject(json);
// QJsonArray封装json数组。QJsonDocument调用array函数得到 QJsonArray对象。
#if 0
QByteArray byteArray = document.toJson(QJsonDocument::Compact); //toJson() 是转换为: JSON字符串, ascii码
#else
QByteArray byteArray = document.toBinaryData(); //toBinaryData() 是转换为: JSON字符串 - 二进制码
#endif
QString strJson(byteArray);
QJsonParseError jsonParseError;
//将一个JSON字符串 转换为JsonObjec对象
QJsonObject ret = QJsonDocument::fromBinaryData(byteArray, QJsonDocument::Validate).object();
QJsonObject re1t = QJsonDocument::fromJson(byteArray, &jsonParseError).object();
if (jsonParseError.error == QJsonParseError::NoError) qDebug() << "success";
//将一个JSON字符串 转换为JsonArray数组
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; // //QJsonParseError 类用于在 JSON 解析中报告错误
QJsonDocument jsonDocument = QJsonDocument::fromJson(jsonString.toLocal8Bit().data(),&jsonError);
if (jsonError.error == QJsonParseError::NoError)
jsonObject = jsonDocument.object();
#else
// 使用QJsonDocument::fromJson()将基于JSON文档的文本形式转换为QJsonDocument对象
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)
{
// 使用QJsonDocument::toJSON()可以将QJsonDocument转换回文本形式
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);//Indented:格式化显示 换行
#else
QString str = json.toJson(QJsonDocument::Compact);//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); //转为Json文档; QJsonDocument可以将一个QJsonArray转换成或转换自一个文本形式的JSON
str = QString(jsonDoc.toJson(QJsonDocument::Compact));//转化为字节数组
#else
QJsonObject jsonObj = doc.object();
QJsonDocument jsonDoc(jsonObj);
str = QString(jsonDoc.toJson(QJsonDocument::Indented));
#endif
return str;
}
复制
转载请注明出处或者链接地址:https://www.qianduange.cn//article/6990.html
标签
pyqt
评论
发布的文章

JQuery中的load()、$

2024-05-10 08:05:15

大家推荐的文章
会员中心 联系我 留言建议 回顶部
复制成功!