QT拼接和解析json

**

更新时间:2020-08-09

**

json所用到的头文件:
#include < QJsonDocument>
#include < QJsonParseError>
#include < QJsonObject>
#include < QJsonArray>
#include < QJsonValue>

拼接json数据

    QJsonObject obj;
    obj.insert("aa","123");
    //带转义参数
    QString strjson= QJsonDocument(obj).toJson(QJsonDocument::Compact);
    qDebug()<<obj;
    qDebug()<<strjson;

在这里插入图片描述
解析普通的json字符串:

    QString str ="{\n    \"aa\": \"123\"\n}\n";
    QJsonParseError parseJsonErr;
    QJsonDocument document = QJsonDocument::fromJson(str.toUtf8(),&parseJsonErr);
    QJsonObject jsonObject = document.object();
    qDebug()<<jsonObject;
    qDebug()<<jsonObject["aa"].toString();

在这里插入图片描述
拼接带数组的json:

    QJsonObject obj;
    QJsonObject obj2;
    obj2.insert("aa","123");
    QJsonArray jsonArray;
    jsonArray.insert(0,obj2);
    obj.insert("aaa",jsonArray);

    QString jsonStr = QJsonDocument(obj).toJson(QJsonDocument::Compact);

    qDebug()<<obj;
    qDebug()<<jsonStr;

在这里插入图片描述

解析带数组的json:
参考:解析json

	QString str = "{ \"student\":[{\"Name\":\"ccf\",\"Math\":\"95\", \"Chinese\":\"84\"}, {\"Name\":\"zhangsan\",\"Math\":\"75\", \"Chinese\":\"70\"}]}";
    
    QJsonParseError parseJsonErr;
    QJsonDocument document = QJsonDocument::fromJson(str.toUtf8(),&parseJsonErr);
    QJsonObject jsonObject = document.object();
    qDebug()<<jsonObject;
    
    if(jsonObject.contains(QStringLiteral("student")))
    {
        QJsonValue arrayValue = jsonObject.value(QStringLiteral("student"));
        
        if(arrayValue.isArray())
        {
            QJsonArray array = arrayValue.toArray();
            qDebug()<<array.size();
            for(int i=0;i<array.size();i++)
            {
                QJsonValue iconArray = array.at(i);
                QJsonObject icon = iconArray.toObject();
                QString id = icon["Name"].toString();
                qDebug()<<"id="<<id;

            }
        }
    }
    

在这里插入图片描述

~持续更新

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Qt中,从文件解析JSON可以使用Qt自带的QFile和QJsonDocument类。下面是一个简单的示例代码: ```cpp #include <QFile> #include <QJsonDocument> #include <QJsonObject> #include <QDebug> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QFile file("example.json"); if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) { qDebug() << "Failed to open file."; return -1; } QByteArray jsonData = file.readAll(); QJsonDocument jsonDocument = QJsonDocument::fromJson(jsonData); if(!jsonDocument.isNull()) { if(jsonDocument.isObject()) { QJsonObject jsonObject = jsonDocument.object(); if(jsonObject.contains("name")) { QJsonValue nameValue = jsonObject.value("name"); if(nameValue.isString()) { QString name = nameValue.toString(); qDebug() << "name:" << name; } } if(jsonObject.contains("age")) { QJsonValue ageValue = jsonObject.value("age"); if(ageValue.isDouble()) { int age = ageValue.toInt(); qDebug() << "age:" << age; } } if(jsonObject.contains("city")) { QJsonValue cityValue = jsonObject.value("city"); if(cityValue.isString()) { QString city = cityValue.toString(); qDebug() << "city:" << city; } } } } file.close(); return a.exec(); } ``` 代码中,首先定义了一个文件名为example.json的文件,然后通过QFile::open()方法打开文件并读取文件中的JSON数据。接着,通过QJsonDocument::fromJson()方法将JSON数据转换为QJsonDocument对象。判断QJsonDocument对象是否为空和是否为对象类型,如果是,就通过QJsonObject类获取JSON对象中的属性值,判断属性值的类型后,获取其值并输出。最后,关闭文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值