Qt QJson组装数据Sig传递
创始人
2025-01-07 21:06:39
0

有时候界面输入的值,不想创建结构体,那么直接用QString类型传输,更便捷方便,速度更快

QJson是你选择的一种方式

组合:

#include  #include  #include  #include  #include   int main(int argc, char *argv[]) {     QCoreApplication a(argc, argv);      // 创建JSON对象     QJsonObject jsonObject;     jsonObject["name"] = "John Doe";     jsonObject["age"] = 30;      // 创建JSON数组     QJsonArray jsonArray;     jsonArray.append("apple");     jsonArray.append("banana");     jsonArray.append("cherry");      // 将数组添加到对象中     jsonObject["fruits"] = jsonArray;      // 创建JSON文档     QJsonDocument jsonDoc(jsonObject);      // 将JSON文档转换为字符串     QString jsonString = jsonDoc.toJson(QJsonDocument::Indented);     qDebug() << "JSON Data:" << jsonString;      return a.exec(); } 

解析:

#include  #include  #include  #include  #include   int main(int argc, char *argv[]) {     QCoreApplication a(argc, argv);      // JSON字符串     QString jsonString = R"(     {         "name": "John Doe",         "age": 30,         "fruits": ["apple", "banana", "cherry"]     })";      // 将JSON字符串解析为JSON文档     QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonString.toUtf8());      // 检查解析是否成功     if (jsonDoc.isNull() || !jsonDoc.isObject()) {         qWarning() << "Failed to parse JSON";         return -1;     }      // 获取JSON对象     QJsonObject jsonObject = jsonDoc.object();      // 访问数据     QString name = jsonObject["name"].toString();     int age = jsonObject["age"].toInt();     QJsonArray fruitsArray = jsonObject["fruits"].toArray();      qDebug() << "Name:" << name;     qDebug() << "Age:" << age;     qDebug() << "Fruits:";      for (const QJsonValue &value : fruitsArray) {         qDebug() << " -" << value.toString();     }      return a.exec(); } 

AI生成,有需求可以联系我

相关内容

热门资讯

透视好友!wepoker挂(透... 透视好友!wepoker挂(透视)永久脚本辅助神器(详细辅助解密教程)1、wepoker挂ai辅助优...
透视系统"wepok... 透视系统"wepoker辅助器"详细辅助安装教程(总是是真的有挂)1.wepoker辅助器 ai辅助...
透视ai!wepoker私人局... 透视ai!wepoker私人局透视方法(透视)永久脚本辅助app(详细辅助wepoke教程)1、we...
透视好友"wepok... 透视好友"wepoker辅助器是真的吗"详细辅助可靠教程(确实是真的有挂)1、wepoker辅助器是...
透视规律!hh poker插件... 透视规律!hh poker插件下载(透视)永久脚本辅助app(详细辅助透明挂教程)1、首先打开hh ...
透视有挂"wepok... 透视有挂"wepoker私人局辅助"详细辅助微扑克教程(都是存在有挂)1、全新机制【wepoker私...
透视透视!德州hhpoker是... 透视透视!德州hhpoker是真的吗(透视)永久脚本辅助挂(详细辅助线上教程)透视透视!德州hhpo...
透视玄学"wepok... 透视玄学"wepoker怎么获得好牌"详细辅助教你教程(其实有挂)1)wepoker怎么获得好牌辅助...
透视美元局!aapoker透视... 透视美元局!aapoker透视脚本下载(透视)永久脚本辅助方法(详细辅助扑克教程)运aapoker透...
透视好友"如何下载德... 透视好友"如何下载德普之星辅助软件"详细辅助黑科技教程(原来是真的有挂)1、上手简单,内置详细流程视...