之前介紹了簡單的示例,這次我們進(jìn)行JSON文件的解析灼狰。
在你的桌面新建文件命名為test.json,內(nèi)容填寫如下:
{
"postsNum" : 2,
"posts" : [
{
"postID" : 10086,
"postTitle" : "hello",
"postContent" : "你好啊"
},
{
"postID" : 10010,
"postTitle" : "hi",
"postContent" : "大家好"
}
]
}
從JSON定義來看,這是一個包含有兩個鍵值對的對象族操,其中一個對象的名字是"postsNum",值是一個數(shù)字比被,另一鍵值對的名字是"posts"色难,值是一個數(shù)組。這個數(shù)組由兩個對象組成等缀,每個對象由三個相同的鍵值對組成枷莉,第一個鍵值對名字為"postID" ,值為數(shù)字尺迂;第二個鍵值對名字為 "postTitle" 笤妙,值為字符串 "hello";第三個鍵值對名字為 "postContent" 噪裕,值為字符串蹲盘。實際上,數(shù)組中的對象描述了一個簡單的帖子的數(shù)據(jù)結(jié)構(gòu)膳音,這個帖子有postID召衔、postTitle、postContent組成祭陷。
接下來我們使用Qt Creator新建一個Qt Console Application項目苍凛,名稱和位置可以隨意趣席。
修改默認(rèn)生成的main.cpp中的代碼如下所示:
#include <QDir>
#include <QFile>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonParseError>
#include <QJsonValue>
#include <QSysInfo>
#include <cstdlib>
#include <iostream>
using std::cout;
int main( int argc, char *argv[] ) {
// Qt的一個宏,消除未使用變量的警告毫深,無實際作用
// 定義為吩坝,#define Q_UNUSED(x) (void)x
Q_UNUSED( argc );
Q_UNUSED( argv );
// 如果當(dāng)前操作系統(tǒng)為windows系統(tǒng)的話
// 修改控制臺的編碼為utf8,防止中文亂碼
if ( QSysInfo::productType() == "windows" ||
QSysInfo::productType() == "winrt" ) {
system( "chcp 65001" );
}
cout << "當(dāng)前使用的操作系統(tǒng)類型為:";
cout << QSysInfo::productType().toStdString() << "\n";
// 默認(rèn)打開的文件位置為用戶桌面的test.txt文件哑蔫。
QFile file( QDir::homePath() + "/Desktop/test.json" );
if ( !file.open( QIODevice::ReadWrite ) ) {
cout << "文件打開失敗!\n";
exit( 1 );
}
cout << "文件打開成功\n";
QJsonParseError jsonParserError;
QJsonDocument jsonDocument =
QJsonDocument::fromJson( file.readAll(), &jsonParserError );
if ( !jsonDocument.isNull() &&
jsonParserError.error == QJsonParseError::NoError ) {
cout << "文件解析成功\n";
if ( jsonDocument.isObject() ) {
QJsonObject jsonObject = jsonDocument.object();
if ( jsonObject.contains( "postsNum" ) &&
jsonObject.value( "postsNum" ).isDouble() ) {
cout << "postsNum is " << jsonObject.value( "postsNum" ).toInt()
<< "\n";
}
if ( jsonObject.contains( "posts" ) &&
jsonObject.value( "posts" ).isArray() ) {
QJsonArray jsonArray = jsonObject.value( "posts" ).toArray();
for ( int i = 0; i < jsonArray.size(); i++ ) {
if ( jsonArray[ i ].isObject() ) {
QJsonObject jsonObjectPost = jsonArray[ i ].toObject();
if ( jsonObjectPost.contains( "postID" ) &&
jsonObjectPost.contains( "postTitle" ) &&
jsonObjectPost.contains( "postContent" ) &&
jsonObjectPost.value( "postID" ).isDouble() &&
jsonObjectPost.value( "postTitle" ).isString() &&
jsonObjectPost.value( "postContent" )
.isString() ) {
cout << "posts[" << i << "] :\n";
cout << "postID is "
<< jsonObjectPost.value( "postID" ).toInt()
<< "\n";
cout << "postTitle is "
<< jsonObjectPost.value( "postTitle" )
.toString()
.toStdString()
<< "\n";
cout << "postContent is "
<< jsonObjectPost.value( "postContent" )
.toString()
.toStdString()
<< "\n";
}
}
}
}
}
}
file.close();
cout << "按任意鍵退出程序\n";
return 0;
}
這段代碼也很容易理解钉寝,首先判斷當(dāng)前系統(tǒng)是否為windows,如果是的話就修改CMD編碼為utf8闸迷,防止出現(xiàn)中文亂碼嵌纲,之后打開桌面上的test.json文件,解析為json文件并輸出里面的數(shù)據(jù)腥沽。