最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

如何在Qt中實(shí)現(xiàn)關(guān)于Json?的操作

 更新時(shí)間:2023年08月07日 10:33:03   作者:小怡的編程之路  
JSON是一種輕量級(jí)數(shù)據(jù)交換格式,常用于客戶端和服務(wù)端的數(shù)據(jù)交互,不依賴于編程語(yǔ)言,在很多編程語(yǔ)言中都可以使用JSON,這篇文章主要介紹了在Qt中實(shí)現(xiàn)關(guān)于Json的操作,需要的朋友可以參考下

前言

一、Json是什么?

1.概念:

JSON(全稱:JavaScript Object Notation),是一種輕量級(jí)的數(shù)據(jù)交換格式。它是 JavaScript 中用于描述對(duì)象數(shù)據(jù)的語(yǔ)法的擴(kuò)展。不過(guò)并不限于與 JavaScript 一起使用。它采用完全獨(dú)立于語(yǔ)言的文本格式,這些特性使 JSON 成為理想的數(shù)據(jù)交換格式。

一般以.json為后綴

2.使用場(chǎng)景:

組織數(shù)據(jù):用于數(shù)據(jù)的網(wǎng)絡(luò)傳輸組織數(shù)據(jù):用于磁盤文件實(shí)現(xiàn)數(shù)據(jù)的持久化存儲(chǔ)

用在登錄 記錄用戶名密碼等,下次打開軟件,自動(dòng)填充。

3.結(jié)構(gòu)和語(yǔ)法:

json語(yǔ)法錯(cuò)誤:

  • 始終將鍵值對(duì)保存在雙引號(hào)內(nèi),大多數(shù) JSON 解析器使用雙引號(hào)解析 JSON 對(duì)象;
  • 切勿在 key 中使用連字符。而是使用下劃線 (_)、全部小寫或駝峰式大小寫;
  • 使用 JSON linter 來(lái)檢查 JSON 是有效的,可以使用 JSONLint 等工具進(jìn)行校驗(yàn)

Json數(shù)組使用[]表示,[]里面是元素,元素間使用 ”,“分隔,最后一個(gè)元素后面,沒有 ,一個(gè)Json數(shù)組,支持多種不同的數(shù)據(jù)類型,包括:整形,浮點(diǎn)型,字符串(string),json數(shù)組,json對(duì)象,空值-null(null)

Json數(shù)組中嵌套Json數(shù)組,父子關(guān)系Json數(shù)組嵌套Json對(duì)象,Json對(duì)象可以嵌套Json數(shù)組

數(shù)組內(nèi)元素類型一致 

[1, 2, 3]
["哈哈","hehe","yiyi"]

數(shù)組內(nèi)元素類型不一致

[1, 2, 3, true, false, "haha",null]

Json數(shù)組和Json對(duì)象嵌套

[//外層是Json數(shù)組
    {//內(nèi)層是Json對(duì)象
        "小明":{//屬于鍵值對(duì)
                       "age":19,
                        "father":"大華",
                         "sister":"小妹"
                   }
         }
]

Json數(shù)組嵌套Json數(shù)組

[
    ["cat", "狗狗", "河馬", 1]//元素類型可以不一致
    [1, 3, 5, true]
]

Json對(duì)象 

Json對(duì)象使用{ }來(lái)描述,每個(gè)Json對(duì)象可以存儲(chǔ)若干個(gè)元素,每個(gè)元素以鍵值對(duì)(key:value)的形式存在,元素與元素之間采用","隔開,最后一個(gè)元素沒有,

注意:

1.鍵值key必須是字符串,且同一層的鍵值不能重復(fù)。

2.value值的類型可選:整形,浮點(diǎn)型,字符串,json數(shù)組,json對(duì)象,空值-null(null)

3.不能亂加否則解析會(huì)出錯(cuò),鍵值不唯一將不能搜到正確的value

Json描述一個(gè)人的信息:
{
        "NAME":"ACE",
        "Sex":"man",
        "Age":20,
        "Family":{
            "Father":"yiyi",
            "brother":["aa","bb","cc"]
        },
        "IsLive":"true"
}

4.在一個(gè).json文件中:只能有一個(gè)Json數(shù)組、只能有一個(gè)Json對(duì)象 

二、在Qt中實(shí)現(xiàn)Json操作的類

使用到以下五個(gè)類:

1.QJsonValue:(Json值)

該類封裝了Json支持的數(shù)據(jù)類型 

//構(gòu)造函數(shù)
QJsonValue(Type type = Null) //初始化一個(gè)空值
QJsonValue(bool b)  //初始化一個(gè)bool值
QJsonValue(double n)   //初始化一個(gè)double 值
QJsonValue(int n)  //初始化一個(gè)int
QJsonValue(qint64 n)   
QJsonValue(const QString &s)   //QString初始化
QJsonValue(const char *s)  //字符串常量初始化
QJsonValue(const QJsonArray &a)   //JsonArray初始化
QJsonValue(const QJsonObject &o)   //JsonObject初始化
//公共函數(shù)
//判斷函數(shù)
bool isArray() const   //判斷value是否是array
bool isBool() const   //判斷是否是bool
bool isDouble() const  //判斷是否是double
bool isNull() const   //判斷是否是空
bool isObject() const   //判斷是否是對(duì)象
bool isString() const   //判斷是否是string
bool isUndefined() const    //判斷是否未定義
//轉(zhuǎn)換函數(shù)
QJsonArray toArray(const QJsonArray &defaultValue) const   //轉(zhuǎn)換為array
QJsonArray toArray() const   //轉(zhuǎn)換成array
bool toBool(bool defaultValue = false) const
double toDouble(double defaultValue = 0) const
int toInt(int defaultValue = 0) const
QJsonObject toObject(const QJsonObject &defaultValue) const
QJsonObject toObject() const
QString toString() const
QString toString(const QString &defaultValue) const
QVariant toVariant() const
Type type() const
bool operator!=(const QJsonValue &other) const
QJsonValue &operator=(const QJsonValue &other)
bool operator==(const QJsonValue &other) const

2.QJsonArray:(Json數(shù)組)

Json數(shù)組是一個(gè)值列表??梢酝ㄟ^(guò)從數(shù)組中插入和刪除QJsonValue來(lái)操作該列表 

//構(gòu)造函數(shù)
QJsonArray()
QJsonArray(std::initializer_list<QJsonValue> args)
QJsonArray(const QJsonArray &other)
//公共函數(shù)
void append(const QJsonValue &value)// 在數(shù)組尾部插入值
QJsonValue at(int i) const
iterator begin()
const_iterator begin() const
const_iterator constBegin() const
const_iterator constEnd() const
bool contains(const QJsonValue &value) const
int count() const
bool empty() const
iterator end()
const_iterator end() const
iterator erase(iterator it)
QJsonValue first() const
void insert(int i, const QJsonValue &value)
iterator insert(iterator before, const QJsonValue &value)
bool isEmpty() const
QJsonValue last() const
void pop_back()
void pop_front()
void prepend(const QJsonValue &value)
void push_back(const QJsonValue &value)
void push_front(const QJsonValue &value)
void removeAt(int i)
void removeFirst()
void removeLast()
void replace(int i, const QJsonValue &value)
int size() const
QJsonValue takeAt(int i)
QVariantList toVariantList() const
//靜態(tài)函數(shù)
QJsonArray fromStringList(const QStringList &list)
QJsonArray fromVariantList(const QVariantList &list)

3.QJsonObject:(Json對(duì)象)

Json對(duì)象是鍵值對(duì)的列表,其中鍵值是唯一的字符串,值由QJsonValue表示 

 
//構(gòu)造函數(shù)
QJsonObject()
QJsonObject(std::initializer_list<QPair<QString, QJsonValue> > args)
QJsonObject(const QJsonObject &other)
//公共函數(shù)
iterator begin()
const_iterator begin() const
const_iterator constBegin() const
const_iterator constEnd() const
const_iterator constFind(const QString &key) const
const_iterator constFind(QLatin1String key) const
bool contains(const QString &key) const
bool contains(QLatin1String key) const
int count() const
bool empty() const
iterator end()
const_iterator end() const
iterator erase(iterator it)
iterator find(const QString &key)
iterator find(QLatin1String key)
const_iterator find(const QString &key) const
const_iterator find(QLatin1String key) const
iterator insert(const QString &key, const QJsonValue &value)
bool isEmpty() const
QStringList keys() const
int length() const
void remove(const QString &key)
int size() const
QJsonValue take(const QString &key)
QVariantHash toVariantHash() const
QVariantMap toVariantMap() const
QJsonValue value(const QString &key) const
QJsonValue value(QLatin1String key) const
bool operator!=(const QJsonObject &other) const
QJsonObject &operator=(const QJsonObject &other)
bool operator==(const QJsonObject &other) const
QJsonValue operator[](const QString &key) const
QJsonValue operator[](QLatin1String key) const
QJsonValueRef operator[](const QString &key)
QJsonValueRef operator[](QLatin1String key)
//靜態(tài)函數(shù)
QJsonObject fromVariantHash(const QVariantHash &hash)
QJsonObject fromVariantMap(const QVariantMap &map)

4.QJsonDocument:(橋梁,轉(zhuǎn)換器作用)

它封裝了一個(gè)完整的Json文檔,并且可以從UTF-8編碼的基于文本的表示以及Qt自己的讀取和寫入該文檔 

//構(gòu)造函數(shù)
QJsonDocument()
QJsonDocument(const QJsonObject &object)  //用QJsonObject初始化
QJsonDocument(const QJsonArray &array)  //用qjsonArray初始化
QJsonDocument(const QJsonDocument &other) //復(fù)制構(gòu)造函數(shù)
~QJsonDocument()
QJsonArray array() const   //轉(zhuǎn)換為QJsonArray
//公共函數(shù)
//判斷函數(shù)
bool isArray() const
bool isEmpty() const
bool isNull() const
bool isObject() const
//轉(zhuǎn)換函數(shù)
QJsonObject object() const // 轉(zhuǎn)換為QJsonObject
const char *rawData(int *size) const
void setArray(const QJsonArray &array)
void setObject(const QJsonObject &object)
QByteArray toBinaryData() const  //轉(zhuǎn)換為二進(jìn)制數(shù)據(jù)
QByteArray toJson(JsonFormat format = Indented) const  //轉(zhuǎn)換為文本文件
QVariant toVariant() const
bool operator!=(const QJsonDocument &other) const
QJsonDocument &operator=(const QJsonDocument &other)
bool operator==(const QJsonDocument &other) const
//靜態(tài)函數(shù)
QJsonDocument fromBinaryData(const QByteArray &data, DataValidation validation = Validate) //從二進(jìn)制文件中讀取數(shù)據(jù)
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error = Q_NULLPTR)//從Json文件中讀取數(shù)據(jù)
QJsonDocument fromRawData(const char *data, int size, DataValidation validation = Validate)
QJsonDocument fromVariant(const QVariant &variant)
 

5.QJsonParseError:(Json錯(cuò)誤)

用于報(bào)告JSON解析期間的錯(cuò)誤 

//公共函數(shù)
QString errorString() const  //Returns the human-readable message appropriate to the reported JSON parsing error.

三、在Qt中實(shí)現(xiàn)Json操作的示例

#include <QCoreApplication>
#include<QJsonValue>
#include<QJsonObject>
#include<QJsonArray>
#include<QDebug>
#include<iostream>
#include<QJsonDocument>
#include<QFile>
#include<QSettings>
using namespace  std;
int number; //全局變量,記錄文件名后綴
QSettings settings("./config.ini",QSettings::IniFormat); //配置文件ini
void WriteJson();//寫文件
void ReadJson();//讀文件
void RecordNumber();//通過(guò)配置文件config.ini獲取當(dāng)前的number值
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    RecordNumber();
    WriteJson();
    system("pause");
    return a.exec();
}
void RecordNumber(){
    number=settings.value("number").toUInt();//通過(guò)配置文件config.ini獲取當(dāng)前的number值
}
void WriteJson(){
   QJsonObject obj;//json對(duì)象
   //json對(duì)象插入值,按照紅黑樹排序
   obj.insert("Name","Ace");
   obj.insert("Sex","man");
   obj.insert("Age",20);
   QJsonObject subObj;//json對(duì)象
   subObj.insert("Father","Gol·D·Roger");
   subObj.insert("Mother","Portgas·D·Rouge");
   QJsonArray  array;//json數(shù)組
   array.append("Sabo");
   array.append("Monkey D. Luffy");
   //在json對(duì)象中嵌套數(shù)組
   subObj.insert("Brother",array);
   //在json對(duì)象中嵌套json對(duì)象
   obj.insert("Family",subObj);
   obj.insert("IsAlive",false);
   obj.insert("comment","yyds");
   QJsonObject objstart;
   objstart.insert("all",obj);//在json對(duì)象中嵌套對(duì)象
   objstart.insert("format","json");
   QJsonDocument doc(objstart);//將json對(duì)象轉(zhuǎn)換為jsonDocument
   QByteArray json=doc.toJson();//轉(zhuǎn)換為json文件
   QFile file(QString::asprintf("C:\\Users\\Administrator\\Desktop\\ace%d.json",number++));//創(chuàng)建json文件
   settings.setValue("number",number);//配置文件中的number值++
   file.open(QIODevice::WriteOnly|QIODevice::Text);
   file.write(json,json.length());//將json字節(jié)數(shù)組寫到文件 當(dāng)中
   file.close(); //關(guān)閉文件
}
void ReadJson(){
}

 得到如下的json文件:

總結(jié)

在保存json文件時(shí)候,json與文件溝通的橋梁QJsonDocument,將json數(shù)據(jù)轉(zhuǎn)換為QByteArray(即字節(jié)數(shù)組),然后保存到.json文件當(dāng)中。

到此這篇關(guān)于在Qt中實(shí)現(xiàn)關(guān)于Json 的操作的文章就介紹到這了,更多相關(guān)Qt中Json 操作內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C++?Qt實(shí)現(xiàn)動(dòng)態(tài)增加垂直滾動(dòng)條

    C++?Qt實(shí)現(xiàn)動(dòng)態(tài)增加垂直滾動(dòng)條

    本博文源于筆者正在工作的一個(gè)小內(nèi)容,內(nèi)容涉及到為qt動(dòng)態(tài)增加垂直滾動(dòng)條,文章分為三個(gè)部分,問題起源,問題解決方案,問題解決成功效果,思路清晰,文章干貨滿滿,復(fù)制源碼即可使用,需要的朋友可以參考下
    2023-08-08
  • C++實(shí)現(xiàn)Date類各種運(yùn)算符重載的示例代碼

    C++實(shí)現(xiàn)Date類各種運(yùn)算符重載的示例代碼

    這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)Date類各種運(yùn)算符重載的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-02-02
  • C語(yǔ)言中的const和free用法詳解

    C語(yǔ)言中的const和free用法詳解

    C語(yǔ)言中的const和C++中的const是有區(qū)別的,而且在使用VS編譯測(cè)試的時(shí)候,如果是C的話,請(qǐng)一定要建立一個(gè)后綴為C的文件,不要是CPP的文件。因?yàn)椋瑑蓚€(gè)編譯器會(huì)有差別的。下面通過(guò)本文給大家分享C語(yǔ)言中的const和free用法,感興趣的朋友一起看看吧
    2017-04-04
  • 在vs2017上配置AppGameKit庫(kù)的圖文教程

    在vs2017上配置AppGameKit庫(kù)的圖文教程

    這篇文章主要介紹了在vs2017上配置AppGameKit庫(kù)的教程,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-04-04
  • C語(yǔ)言小程序 如何判斷兩個(gè)日期之差

    C語(yǔ)言小程序 如何判斷兩個(gè)日期之差

    輸入兩個(gè)日期,計(jì)算之間相差多少天。 用了兩種方法實(shí)現(xiàn),第二種利用結(jié)構(gòu)體,代碼比較清晰,其余的都一樣
    2013-07-07
  • C/C++中宏定義(#define)

    C/C++中宏定義(#define)

    #define命令是C語(yǔ)言中的一個(gè)宏定義命令,它用來(lái)將一個(gè)標(biāo)識(shí)符定義為一個(gè)字符串,該標(biāo)識(shí)符被稱為宏名,被定義的字符串稱為替換文本。接下拉通過(guò)本文給大家分享C/C++中宏定義(#define)知識(shí),需要的朋友參考下
    2017-02-02
  • C++中的覆蓋和隱藏詳解

    C++中的覆蓋和隱藏詳解

    這篇文章主要介紹了C++中重載、重寫(覆蓋)和隱藏的區(qū)別,是C++面向?qū)ο蟪绦蛟O(shè)計(jì)非常重要的概念,需要的朋友可以參考下,希望能夠給你帶來(lái)幫助
    2021-08-08
  • C語(yǔ)言解決字符串中插入和刪除某段字符串問題

    C語(yǔ)言解決字符串中插入和刪除某段字符串問題

    這篇文章主要介紹了C語(yǔ)言解決字符串中插入和刪除某段字符串問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • c++ vector 常用函數(shù)示例解析

    c++ vector 常用函數(shù)示例解析

    這篇文章主要介紹了c++ vector 常用函數(shù)示例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • vscode調(diào)試gstreamer源碼的詳細(xì)流程

    vscode調(diào)試gstreamer源碼的詳細(xì)流程

    在本文中主要介紹了如何使用vscode調(diào)試C++和python程序,并進(jìn)一步分析了如何調(diào)試gstreamer源碼,講述了如何調(diào)試gstreamer源碼的具體流程,感興趣的朋友跟隨小編一起看看吧
    2023-01-01

最新評(píng)論

南康市| 乌拉特后旗| 长丰县| 商城县| 遂川县| 德格县| 滨海县| 环江| 鄂托克旗| 河北省| 拉萨市| 乌拉特中旗| 沙雅县| 岑巩县| 镇康县| 吴川市| 镇康县| 那坡县| 资源县| 石家庄市| 峨眉山市| 松滋市| 邓州市| 乐平市| 射洪县| 姚安县| 衡阳市| 东阿县| 喀喇沁旗| 玉环县| 临漳县| 康定县| 吴川市| 乌拉特中旗| 鄂尔多斯市| 阳信县| 宁河县| 阜城县| 长春市| 阜南县| 通城县|