zoukankan      html  css  js  c++  java
  • JSON

    1.关于JSON的介绍参考www.json.org

    Qt提供处理JSON数据的支持。
    QJSonObject类用于封装JSON object;
    QJsonDocument类提供读写JSON文档的方法;
    QJsonParseError类用于在JSON解析过程中报告错误。
    上述三个类均是从Qt 5.0开始支持。

    示例:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QJsonObject>
    #include <QJsonDocument>
    #include <QJsonParseError>
    #include<QDebug>
    
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
      QJsonObject json;
      json.insert("Type",QString("Rectangle"));
      json.insert("widght",42);
       json.insert("height",23);
    
       QJsonDocument document;
       document.setObject(json);
       QByteArray byteArray = document.toJson(QJsonDocument::Compact);
       qDebug()<<byteArray;
    
        QJsonParseError jsonError;
        QJsonDocument parseDoc = QJsonDocument::fromJson(byteArray,&jsonError);
        if(jsonError.error == QJsonParseError::NoError)
        {
            if(parseDoc.isObject())
            {
                QJsonObject jsonObj = parseDoc.object();
                if(jsonObj.contains("Type"))
                {
                    QJsonValue typeValue = jsonObj.take("Type");
                    if(typeValue.isString())
                    {
                        QString strValue= typeValue.toString();
                        qDebug()<<"Type : "<<strValue;
                    }
                }
                if(jsonObj.contains("height"))
                {
                    QJsonValue heightValue = jsonObj.take("height");
                    if(heightValue.isDouble())
                    {
                        int iValue = heightValue.toVariant().toInt();
                        qDebug()<<"height : "<<iValue;
                    }
                }
                if(jsonObj.contains("widght"))
                {
                    QJsonValue widghtValue = jsonObj.take("widght");
                    if(widghtValue.isDouble())
                    {
                        int iValue =widghtValue.toVariant().toInt();
                        qDebug()<<"widght : "<<iValue;
                    }
                }
            }
        }
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }

    结果如下:

    "{"Type":"Rectangle","height":23,"widght":42}"
    Type :  "Rectangle"
    height :  23
    widght :  42
  • 相关阅读:
    Oracle启动关闭
    Open_stack 有虚拟机端口不通的问题
    关于Oracle归档的一些操作
    电脑无法开机 接通电源后主板有红灯闪烁的问题
    Centos7+python3.6+face-recognition
    电脑无法开机的问题-主板上有红色告警灯闪烁
    关于systemctl
    Vsftp搭建 for centos7
    外星人入侵——安装Pygame
    mysql索引原理详解
  • 原文地址:https://www.cnblogs.com/Pan-Z/p/6425121.html
Copyright © 2011-2022 走看看