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生成,有需求可以联系我

相关内容

热门资讯

透视方针!哈糖大菠萝有没有挂,... 透视方针!哈糖大菠萝有没有挂,hhpoker有没有作必弊辅助(透视)总是是真的挂(哔哩哔哩)1、完成...
透视大纲!wepoker有什么... 透视大纲!wepoker有什么规律,wepoker国外版透视(透视)原来真的有脚本神器(哔哩哔哩)1...
透视方式!佛手在线有挂吗,we... 透视方式!佛手在线有挂吗,wepoker辅助真的假的(透视)一直是真的挂(哔哩哔哩)暗藏猫腻,小编详...
透视模板!wpk软件是真的吗,... 透视模板!wpk软件是真的吗,wpk私人局有透视吗(透视)一贯真的有脚本工具(哔哩哔哩)1、完成wp...
透视教程书!佛手大菠萝13道挂... 透视教程书!佛手大菠萝13道挂哪里,hhpoker作必弊码(透视)都是有挂(哔哩哔哩)暗藏猫腻,小编...
透视举措!aa poker透视... 透视举措!aa poker透视软件,aapoker破解侠是真的吗(透视)其实是有脚本神器(哔哩哔哩)...
透视诀窍!pokemmo脚本辅... 透视诀窍!pokemmo脚本辅助下载,德州辅助工具到底怎么样(透视)好像是有挂(哔哩哔哩)1、起透看...
透视举措!hhpoker德州作... 透视举措!hhpoker德州作必弊,hhpoker怎么破解(透视)确实真的有脚本神器(哔哩哔哩)1、...
透视烘培!xpoker辅助神器... 透视烘培!xpoker辅助神器,wpk透视辅助(透视)果然是有挂(哔哩哔哩)该软件可以轻松地帮助玩家...
透视演示!hhpoker辅助挂... 透视演示!hhpoker辅助挂下载,hhpoker辅助软件下载(透视)竟然是有透视攻略(哔哩哔哩)1...