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

相关内容

热门资讯

透视要领!智星德州可以透视吗,... 您好,智星德州可以透视吗这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很多...
透视项目!xpoker辅助神器... 透视项目!xpoker辅助神器(透视)开挂脚本安装(哔哩哔哩)运xpoker辅助神器辅助工具,进入游...
实测揭晓!"大菠萝辅... 实测揭晓!"大菠萝辅助挂开挂"切实是有辅助脚本(有挂教程)-哔哩哔哩1、让任何用户在无需大菠萝辅助挂...
透视指南书!wepoker透视... 透视指南书!wepoker透视破解版,hhpoker免费透视脚本(透视)切实真的是有挂(哔哩哔哩)1...
透视妙招!hhpoker软件可... 透视妙招!hhpoker软件可以玩吗(透视)开挂脚本神器(哔哩哔哩)1、实时hhpoker软件可以玩...
透视绝活!epoker透视底牌... 透视绝活!epoker透视底牌,来玩app破解(透视)确实有脚本挂(哔哩哔哩)运epoker透视底牌...
一分钟揭秘!"广西老... 一分钟揭秘!"广西老友玩插件"真是是有辅助神器(有挂头条)-哔哩哔哩1、游戏颠覆性的策略玩法,独创攻...
透视窍门!wpk插件,线上德州... 透视窍门!wpk插件,线上德州的辅助器是什么(透视)一贯真的有挂(哔哩哔哩)透视窍门!wpk插件,线...
透视妙招!aapoker插件(... 透视妙招!aapoker插件(透视)开挂透视软件(哔哩哔哩)1、下载好aapoker插件透视辅助下载...
透视法子!hhpoker免费辅... 透视法子!hhpoker免费辅助器,德州hhpoker脚本(透视)一贯是有脚本工具(哔哩哔哩)进入游...