huweide

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
#include <QCoreApplication>
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonValue>
#include <QJsonDocument>
#include <QFile>
#include <QDebug>
#pragma execution_character_set("utf-8")

void WriteJson()
{
    //构建根节点
    QJsonObject m_rootObject;

    //根节点添加键值
    m_rootObject.insert("Name","超兽武装");

    //构建信息对象
    QJsonObject infoObject;
    infoObject.insert("Direct"," 王巍");
    infoObject.insert("Number",2);
    infoObject.insert("Is_end",true);

    //根节点添加信息对象
    m_rootObject.insert("Info",infoObject);

    //构建成员数组
    QJsonArray MemberArray;

    //构建成员对象
    QJsonObject HLF;
    HLF.insert("Name","火麟飞");
    HLF.insert("Superbeast","幻麟兽");
    //构建成员对象
    QJsonObject LJ;
    LJ.insert("Name","龙戬");
    LJ.insert("Superbeast","龙战兽");

    //成员数组添加成员对象,append函数
    MemberArray.append(HLF);
    MemberArray.append(LJ);

    //根节点添加成员数组
    m_rootObject.insert("member",MemberArray);

    //打印输出
    QJsonDocument doc(m_rootObject);
    QByteArray json = doc.toJson();
    qDebug() << QString(json).toUtf8().data();

    //保存为文件
    QFile file("E:/SC/json/demo.json");
    file.open(QFile::WriteOnly);
    file.write(json);
    file.close();
}

void FromJson()
{
    //读取文件
    QFile file("E:/SC/json/demo.json");
    file.open(QFile::ReadOnly);
    QByteArray json = file.readAll();
    file.close();

    //创建QJsonDocument
    QJsonDocument doc = QJsonDocument::fromJson(json);
    if(!doc.isObject())
    {
        qDebug() << " 这不是一个json文件";
        return ;
    }
    //转化成对象
    QJsonObject obj = doc.object();
    //获取所有的键
    QStringList keys = obj.keys();

    for(int i = 0;i< keys.count();i++)
    {//获取 key 与 value
       QString key = keys[i];
       QJsonValue value = obj.value(key);
       if(value.isBool())
       {
           qDebug() << key << value.toBool();
       }
       else if(value.isDouble())
       {
           qDebug() << key << value.toDouble();
       }
       else if(value.isString())
       {
           qDebug() << key << value.toString();
       }
       else if(value.isObject())
       {
           qDebug() << key << ":";
           QJsonObject infoObject = value.toObject();
           QString Direct = infoObject["Direct"].toString();
           bool Is_end = infoObject["Is_end"].toBool();
           int Number = infoObject["Number"].toInt();

           qDebug() << " " << "Direct" << Direct;
           qDebug() << " " << "Is_end" << Is_end;
           qDebug() << " " << "Number" << Number;
       }
       else if(value.isArray())
       {
           QJsonArray memberArray = value.toArray();
           for(int i = 0;i<memberArray.size();i++)
           {
               QJsonObject member = memberArray[i].toObject();
               QString Superbeast = member["Superbeast"].toString();
               QString Name = member["Name"].toString();

               qDebug() <<" Superbeast :" << Superbeast << ",Name" << Name;
           }
       }
    }
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    WriteJson();
    FromJson();
    return a.exec();
}

 

posted on 2023-03-09 22:06  奇怪的菜鸟  阅读(19)  评论(0)    收藏  举报