QT-json文件

QTjson文件的简单使用

一、创建和写入json文件

1、引用的头文件

复制代码
#include <QJsonObject>
#include <QJsonDocument>

2、数据转json代码

复制代码
 //1.创建json对象
    QJsonObject info;

    info.insert("name", "xiaoming");
    info.insert("age", 18);
    info.insert("height", 185);
    info.insert("weight", "75Kg");

    QJsonObject jsoninfo;
    jsoninfo.insert("code", 1);
    jsoninfo.insert("info", "个人信息");
    jsoninfo.insert("data", info);

    //2.创建json文档
    QJsonDocument jsondoc;
    jsondoc.setObject(jsoninfo);

    QFile qfile("./info.json");
    if(qfile.open(QIODevice::WriteOnly)){
        qfile.write(jsondoc.toJson());
        qfile.close();
        qDebug()<<"写入json文件成功";
        QMessageBox::information(this, "json", "信息写入json文件成功");
    }else{
        QMessageBox::warning(this, "写入文件", "文件打开失败");
    }

二、读取json文件

复制代码
QString strjson;
    QFile qfile("./info.json");
    if(qfile.open(QIODevice::ReadOnly)){
        strjson = qfile.readAll();
        qfile.close();

    }else{
        QMessageBox::warning(this, "读取文件", "文件打开失败");
        exit(1);
    }

    QJsonParseError parseerr;   //返回解析错误的报告信息
    QJsonDocument jsoninfo = QJsonDocument::fromJson(strjson.toUtf8(), &parseerr);

    if(!jsoninfo.isEmpty() && (parseerr.error == QJsonParseError::NoError)){
        //返回的json不为空,没有解析错误
        //转为json对象
        QJsonObject json = jsoninfo.object();
        QJsonValue code = json.value("code");
        QJsonValue data = json.value("data");
        if(code.isUndefined() || code.toDouble() != 1 || data.isUndefined() || !data.isObject()){
            QMessageBox::warning(this, "wearing", "json数据转换错误");
            exit(100);
        }

        QJsonObject info = data.toObject();

        /*
    info.insert("name", "xiaoming");
    info.insert("age", 18);
    info.insert("height", 185);
    info.insert("weight", "75Kg");
    */

        QJsonValue name = info.value("name");
        QJsonValue age = info.value("age");
        QJsonValue height = info.value("height");
        QJsonValue weight = info.value("weight");
        if(name.isUndefined() ||
                age.isUndefined()||
                height.isUndefined()||
                weight.isUndefined()){
            QMessageBox::critical(this, "error", "接口错误");
            exit(100);
        }

        QString strname = name.toString();
        int intage = age.toInt();
        int intheight = height.toInt();
        int intweight = weight.toInt();

        QString strinfo = "info:";
        strinfo += "\nname:"+strname;
        strinfo += "\nage:"+QString::number(intage, 10);
        strinfo += "\nintheight:"+QString::number(intheight, 10);
        strinfo += "\nintweight:"+QString::number(intweight, 10);
        QMessageBox::information(this, "infotojson", strinfo, QMessageBox::Yes);

    }else{
        QMessageBox::warning(this, "str2jsondoc", "json解析失败");
        exit(100);
    }
相关推荐
小短腿的代码世界3 分钟前
Qt日志系统深度解析:从qDebug到企业级日志框架
开发语言·qt
REDcker1 小时前
浏览器端Web程序性能分析与优化实战 DevTools指标与工程清单
开发语言·前端·javascript·vue·ecmascript·php·js
我命由我123452 小时前
Kotlin 开发 - lateinit 关键字
android·java·开发语言·kotlin·android studio·android-studio·android runtime
Halo_tjn2 小时前
Java Set集合相关知识点
java·开发语言·算法
许彰午3 小时前
我手写了一个 Java 内存数据库(二):B+ 树的插入与分裂
java·开发语言·面试
大飞记Python3 小时前
【2026更新】Python基础学习指南(AI版)——04数据类型
开发语言·人工智能·python
Alice-YUE4 小时前
【js高频八股】防抖与节流
开发语言·前端·javascript·笔记·学习·ecmascript
云泽8084 小时前
C++11 核心特性全解:列表初始化、右值引用与移动语义实战
开发语言·c++
froginwe114 小时前
DOM 加载函数
开发语言
Hello eveybody4 小时前
介绍一下背包DP(Python)
开发语言·python·动态规划·dp·背包dp