久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在 Qt5 中創(chuàng)建/讀取/寫(xiě)入 JSON 文件

How to create/read/write JSON files in Qt5(如何在 Qt5 中創(chuàng)建/讀取/寫(xiě)入 JSON 文件)
本文介紹了如何在 Qt5 中創(chuàng)建/讀取/寫(xiě)入 JSON 文件的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

Qt5 有一個(gè)新的 JSON 解析器,我想使用它.問(wèn)題在于,外行人對(duì)這些函數(shù)的作用以及如何用它編寫(xiě)代碼并不太清楚.否則我可能讀錯(cuò)了.

Qt5 has a new JSON parser and I want to use it. The problem is that it isn't too clear about what the functions do in layman's terms and how to write code with it. That or I could be reading it wrong.

我想知道在 Qt5 中創(chuàng)建 JSON 文件的代碼以及什么封裝"了意思是.

I want to know the code on creating a JSON file in Qt5 and what "encapsulates" mean.

推薦答案

示例:從文件中讀取 json

/* test.json */
{
   "appDesc": {
      "description": "SomeDescription",
      "message": "SomeMessage"
   },
   "appName": {
      "description": "Home",
      "message": "Welcome",
      "imp":["awesome","best","good"]
   }
}


void readJson()
   {
      QString val;
      QFile file;
      file.setFileName("test.json");
      file.open(QIODevice::ReadOnly | QIODevice::Text);
      val = file.readAll();
      file.close();
      qWarning() << val;
      QJsonDocument d = QJsonDocument::fromJson(val.toUtf8());
      QJsonObject sett2 = d.object();
      QJsonValue value = sett2.value(QString("appName"));
      qWarning() << value;
      QJsonObject item = value.toObject();
      qWarning() << tr("QJsonObject of description: ") << item;

      /* in case of string value get value and convert into string*/
      qWarning() << tr("QJsonObject[appName] of description: ") << item["description"];
      QJsonValue subobj = item["description"];
      qWarning() << subobj.toString();

      /* in case of array get array and convert into string*/
      qWarning() << tr("QJsonObject[appName] of value: ") << item["imp"];
      QJsonArray test = item["imp"].toArray();
      qWarning() << test[1].toString();
   }

<小時(shí)>

輸出

QJsonValue(object, QJsonObject({"description": "Home","imp": ["awesome","best","good"],"message": "YouTube"}) ) 
"QJsonObject of description: " QJsonObject({"description": "Home","imp": ["awesome","best","good"],"message": "YouTube"}) 
"QJsonObject[appName] of description: " QJsonValue(string, "Home") 
"Home" 
"QJsonObject[appName] of value: " QJsonValue(array, QJsonArray(["awesome","best","good"]) ) 
"best" 

<小時(shí)>

示例:從字符串中讀取 json

將 json 分配給字符串,如下所示,并使用前面顯示的 readJson() 函數(shù):

Assign json to string as below and use the readJson() function shown before:

val =   
'  {
       "appDesc": {
          "description": "SomeDescription",
          "message": "SomeMessage"
       },
       "appName": {
          "description": "Home",
          "message": "Welcome",
          "imp":["awesome","best","good"]
       }
    }';

<小時(shí)>

輸出

QJsonValue(object, QJsonObject({"description": "Home","imp": ["awesome","best","good"],"message": "YouTube"}) ) 
"QJsonObject of description: " QJsonObject({"description": "Home","imp": ["awesome","best","good"],"message": "YouTube"}) 
"QJsonObject[appName] of description: " QJsonValue(string, "Home") 
"Home" 
"QJsonObject[appName] of value: " QJsonValue(array, QJsonArray(["awesome","best","good"]) ) 
"best" 

這篇關(guān)于如何在 Qt5 中創(chuàng)建/讀取/寫(xiě)入 JSON 文件的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數(shù)據(jù)?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫(xiě) for() 循環(huán): for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環(huán)?)
Reusing thread in loop c++(在循環(huán) C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環(huán)形?)
主站蜘蛛池模板: 欧美九九| 91久久国产综合久久 | 国产亚洲精品a | 天堂色区 | 国产一区二区三区在线视频 | 一区二区视频在线 | 欧美成人免费在线 | 久久精品一级 | 羞羞视频在线观看免费观看 | 国产在线一区二区 | 99精品国产一区二区三区 | 特黄一级| 日产久久| 久久激情五月丁香伊人 | 精品欧美乱码久久久久久 | 久久中文视频 | 91成人免费观看 | 91社影院在线观看 | 国产日韩精品视频 | 欧美激情a∨在线视频播放 成人免费共享视频 | 伊人激情综合网 | 亚洲精品成人 | 久草色视频 | 日韩欧美中文字幕在线视频 | 久久精品欧美一区二区三区不卡 | 国产一区二区三区在线看 | 操射视频| 91麻豆精品国产91久久久更新资源速度超快 | pacopacomama在线| 国产精品av久久久久久久久久 | 一区二区三区在线免费观看 | 欧美日韩久久 | 91社区在线观看 | 国产高清无av久久 | 天天射色综合 | 亚洲欧美一区二区三区在线 | 亚洲欧美视频一区 | 亚洲精品久久久9婷婷中文字幕 | 91九色婷婷 | 欧美午夜精品 | 成人免费观看视频 |