首页 > 其他分享 >Qt Json 处理

Qt Json 处理

时间:2023-03-20 13:44:42浏览次数:35  
标签:jsonObj Qt 处理 value Json jsonDoc include QJsonDocument

chatgpt

#include <QJsonDocument>
#include <QJsonObject>
#include <QDebug>

// 从 JSON 字符串中解析数据
void parseJson(const QString& jsonString)
{
    // 将 JSON 字符串转换为 QJsonDocument 对象
    QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonString.toUtf8());

    // 判断转换是否成功
    if (!jsonDoc.isNull() && jsonDoc.isObject()) {
        // 获取 QJsonObject 对象
        QJsonObject jsonObj = jsonDoc.object();

        // 获取指定字段的值
        int id = jsonObj.value("id").toInt();
        QString name = jsonObj.value("name").toString();
        bool isEnabled = jsonObj.value("isEnabled").toBool();

        // 输出结果
        qDebug() << "id:" << id;
        qDebug() << "name:" << name;
        qDebug() << "isEnabled:" << isEnabled;
    } else {
        qDebug() << "JSON 格式错误";
    }
}

// 将数据转换为 JSON 字符串
void toJsonString()
{
    // 创建 QJsonObject 对象
    QJsonObject jsonObj;
    jsonObj.insert("id", 123);
    jsonObj.insert("name", "张三");
    jsonObj.insert("isEnabled", true);

    // 将 QJsonObject 对象转换为 QJsonDocument 对象
    QJsonDocument jsonDoc(jsonObj);

    // 将 QJsonDocument 对象转换为 JSON 字符串
    QString jsonString = QString::fromUtf8(jsonDoc.toJson());

    // 输出结果
    qDebug() << jsonString;
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    // 解析 JSON 数据
    QString jsonString = "{\"id\":123, \"name\":\"张三\", \"isEnabled\":true}";
    parseJson(jsonString);

    // 将数据转换为 JSON 字符串
    toJsonString();

    return a.exec();
}

标签:jsonObj,Qt,处理,value,Json,jsonDoc,include,QJsonDocument
From: https://www.cnblogs.com/simp/p/17235998.html

相关文章

  • element的图片上传预处理函数
      /**图片格式和大小的控制*/beforeAvatarUpload(file){//允许上传jpg和png格式的图片constisJPG=file.type==="image/jpeg"||f......
  • JavaScript解析JSON
    一个对象以“{”开始,“}”结束。每个“key”后跟一“:”,“‘key/value’对”之间运用“,”分隔。遍历JSON对象中的数据,可通过for-in循环实现。数据{"主题":{......
  • Qt5.12实战之QByteArray数据转换处理
    效果:视频:​​https://www.kuaishou.com/short-video/3xje8ib9gj68u52?authorId=3x8b3wqc6r8q766&streamSource=profile&area=profilexxnull​​示例源码:#include<QCo......
  • Qt5.12实战之基本数据类型
    注:macosx下要加上#include<QIODevice>//formacosxendl替换为Qt::endl;示例源码:#include<QCoreApplication>#include<QDebug>#include<QTextStream>stat......
  • Mac安装office2021套件 Mac原生版本教程(支持M1M2系列和Intel处理器)
    支持Mac的原生版本office2021来了,更新后的Mac原生版office2021适配了AppleM系列的处理器,运行速度提升更快、效率更高~office2021Mac原生激活版本Office2021Mac中文激......
  • 四、第一个opengl的qt程序
    原链接1、QOpenGLWidget:不需要GLFWQOpenGLWdiget提供了三个便捷的虚函数,可以重载,用来重新实现典型的OpenGL任务:.panitGL:渲染OpenGL场景。widget需要更新时调用。.resiz......
  • SpringMVC返回JSON对象遇到的错误
    SPringMVC返回JSON对象遇到报错:org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.logExceptionResolved[org.springframework.http.converter......
  • JSONP、XHR的属性
    JSONP1、JSONP的原理script标签跨域不会被浏览器阻止JSONP主要就是利用scrip标签,加载跨域文件2、使用JSONP实现跨域服务端准备好JSONP接口https://www.imooc.com/api/......
  • [Android] Android版本 和 SDK / NDK / Qt 版本对应关系
    系统版本与SDK版本平台版本 API级别(SDK版本)版本代号发布时间Android1130R2020.9Android10.029 Q2019.8Android9.028Pie2018.8Android8.1 27 Oreo2017.12Android8.0......
  • Qt5.12实战之QByteArray与字符指针及字符串转换
    示例源码:#include<QCoreApplication>#include<QDebug>#include<QTextStream>staticQTextStreamcout(stdout,QIODevice::WriteOnly);#include<iostream>#include......