QT中有解析Json的一個類叫QScript。貌似還有一個QJson,但據說解析的方便性不如QScript,具體沒有深刻探究,這裏僅簡單記錄一下QScript的使用。json
首先,主要使用到的類有QScriptEngine和QScriptValue,這裏寫了一個Demo,是用來將一個存有Json語句的QString解析後存入XML裏並在Debug裏顯示出來。因爲理論知識裝備的並不充足,因此就先實戰一下吧。寫入XML方法使用的是QXmlStreamWriter類。數組
首先,建議使用窗口,即便是空窗口。由於這樣當你關閉窗口的時候程序就會自動結束。那使用以下代碼呢?函數
1
2
3
4
5
6
|
int
main(
int
argc,
char
*argv[]){
dbg<<
"This is a Test !"
;
QScriptJson qsj(
"D:\\json.xml"
);
//QScriptJson是接下來本身定義的類
qsj.write2File();
return
0;
}
|
NO !會出現下面的錯誤信息:this
QScriptEngine: Must construct a Q(Core)Application before a QScriptEnginelua
意思就是要使用QScriptEngine,必需要先構造一個Q(Core)Application,因此老老實實構造一個吧。spa
下面是個人程序代碼:code
【AHeaders.h】加上了要使用的頭文件orm
1
2
3
4
5
6
7
8
9
10
11
|
#ifndef AHEADERS_H
#define AHEADERS_H
#include <QDebug>
#include <QVariant>
#include <QString>
#include <QStringList>
#include <QXmlStreamWriter>
#define dbg qDebug()
#endif // AHEADERS_H
|
【qscriptjson.h】xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
#ifndef QSCRIPTJSON_H
#define QSCRIPTJSON_H
#include <QScriptEngine>
#include <QScriptValue>
#include <QScriptValueIterator>
#include <QFile>
#include <QMap>
#include "AHeaders.h"
class
QScriptJson
{
public
:
QScriptJson(
const
QString &fileName);
~QScriptJson();
bool
write2File();
private
:
void
getPropety(QScriptValue sv, QStringList sLsPty);//輸出到XML
void
getPropety(QScriptValue sv, QStringList sLsPty,QString space);//輸出到Debug
private
:
QXmlStreamWriter m_writer;
QString m_fileName;
QMap<QString,QVariant> m_mapProperty;
//存放數據層次屬性
QMap<QString,QVariant> m_mapItems;
//節點中的子節點集
};
#endif // QSCRIPTJSON_H
|
【qscriptjson.cpp】ip
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
#include "qscriptjson.h"
//要處理的數據
static
QString datas =
QString("{
'billcount'
:2,
'bills'
:[\
{
'billID'
:
'010101001'
,
'billname'
:
'平整場地'
,
'billunit'
:
'm2'
,
'decount'
:3,\
'des'
:[{
'deID'
:
'A1-1'
,
'decont'
:
'建築場地挖填高度在±30cm之內的找平'
,
'dename'
:
'平整場地'
,
'deunit'
:
'100m2'
},\
{
'deID'
:
'A1-3'
,
'decont'
:
'土方開挖'
,
'dename'
:
'人工挖土方 三類土 深度在1.5m內'
,
'deunit'
:
'100m3'
},\
{
'deID'
:
'A1-4'
,
'decont'
:
'土方開挖'
,
'dename'
:
'人工挖土方 四類土 深度在1.5m內'
,
'deunit'
:
'100m3'
}]},\
{
'billID'
:
'010101005'
,
'billname'
:
'挖淤泥、流砂'
,
'billunit'
:
'm3'
,
'decount'
:1,\
'des'
:[{
'deID'
:
'A1-27'
,
'decont'
:
'挖淤泥、流砂'
,
'dename'
:
'人工挖淤泥流砂'
,
'deunit'
:
'100m3'
}]}]}");
QScriptJson::QScriptJson(
const
QString &fileName)
{
m_fileName=fileName;
QStringList sls;
m_mapProperty.insert(
"_BEGIN_"
,
"BillTable"
);
sls<<
"billcount"
<<
"bills"
;
m_mapProperty.insert(
"BillTable"
,sls);
sls.clear();
sls<<
"billID"
<<
"billname"
<<
"billunit"
<<
"decount"
<<
"des"
;
m_mapProperty.insert(
"bills"
,sls);
sls.clear();
sls<<
"deID"
<<
"decont"
<<
"dename"
<<
"deunit"
;
m_mapProperty.insert(
"des"
,sls);
sls.clear();
m_mapProperty.insert(
"_END_"
,
"_END_"
);
m_mapItems.insert(
"bills"
,
"BillItem"
);
m_mapItems.insert(
"des"
,
"DesItem"
);
}
QScriptJson::~QScriptJson()
{
}
bool
QScriptJson::write2File()
{
QString dt=datas;
QFile file(m_fileName);
if
(!file.open(QFile::WriteOnly | QFile::Text)) {
dbg<<QString(
"打不開文件 : %1"
).arg (m_fileName);
return
false
;
}
try
{
m_writer.setDevice(&file);
m_writer.setAutoFormatting(
true
);
//文檔開始
m_writer.writeStartDocument();
QScriptEngine engine;
QScriptValue sv = engine.evaluate(
"value="
+dt);
QString strBegin=m_mapProperty.value(
"_BEGIN_"
).toString();
m_writer.writeStartElement(strBegin);
QStringList sLsPty=m_mapProperty.value(strBegin).toStringList();
getPropety(sv,sLsPty,
" "
);
getPropety(sv,sLsPty);
m_writer.writeEndElement();
m_writer.writeEndDocument();
}
catch
(...){
dbg<<
"Error: Write to File ."
;
file.close();
}
file.close();
}
void
QScriptJson::getPropety(QScriptValue sv,QStringList sLsPty)
{
foreach(QString element,sLsPty){
QScriptValue sptV=sv.property(element);
if
(!sptV.isArray()){
QString value=sptV.toString();
m_writer.writeAttribute(element,value);
}
else
{
m_writer.writeStartElement(element);
QScriptValueIterator it(sptV);
while
(it.hasNext()) {
it.next();
if
(it.flags()&&QScriptValue::SkipInEnumeration)
continue
;
QString item=m_mapItems.value(element).toString();
m_writer.writeStartElement(item);
QStringList sLs_Child=m_mapProperty.value(element).toStringList();
getPropety(it.value(),sLs_Child);
m_writer.writeEndElement();
}
m_writer.writeEndElement();
}
}
}
void
QScriptJson::getPropety(QScriptValue sv, QStringList sLsPty,QString space)
{
foreach(QString str,sLsPty){
QScriptValue sptV=sv.property(str);
if
(!sptV.isArray()){
QString str2=sptV.toString();
dbg<<(space+str+QString(
" : "
)+str2);
//若是當前屬性不是一個數組,則輸出其內容
}
else
{
dbg<<(space+str+QString(
" [ "
));
QScriptValueIterator it(sptV);
// dbg<<it.name()<<it.value().toString();
// if(it.hasNext())it.next();
while
(it.hasNext()) {
it.next();
// dbg<<it.name()<<it.value().toString();
if
(it.flags()&&QScriptValue::SkipInEnumeration)
continue
;
QStringList sLs_Child=m_mapProperty.value(str).toStringList();
getPropety(it.value(),sLs_Child,space+
" "
);
// it.next();
}
// dbg<<it.name()<<it.value().toString();
dbg<<(space+str+QString(
" ] "
));
}
}
}
|
至此,主要文件結束,下面是一個窗口文件:
【mainwindow.h】
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTextEdit>
class
MainWindow :
public
QMainWindow
{
Q_OBJECT
public
:
MainWindow(QString strText, QWidget *parent = 0);
~MainWindow();
void
setText(QString strText);
private
:
QTextEdit *m_te;
QString m_text;
};
#endif // MAINWINDOW_H
|
【mainwindow.cpp】
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#include "mainwindow.h"
MainWindow::MainWindow(QString strText,QWidget *parent)
: QMainWindow(parent)
{
m_text=strText;
m_te=
new
QTextEdit(
this
);
m_te->setText(m_text);
this
->setCentralWidget(m_te);
}
MainWindow::~MainWindow()
{
delete
m_te;
}
void
MainWindow::setText(QString strText)
{
m_text=m_text+
"\n"
+strText;
m_te->setText(m_text);
}
|
最後上Main函數!
【main.cpp】
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#
include
"mainwindow.h"
#
include
<QApplication>
#
include
"qscriptjson.h"
int
main(
int
argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w(
"程序開始。。。"
);
w.show();
dbg<<
"This is a Test !"
;
QScriptJson qsj(
"D:\\json.xml"
);
qsj.write2File();
w.setText(
"程序已運行完成!\n\n關閉窗口退出程序。。。"
);
return
a.exec();
}
|
運行後生成的XML文檔「json.xml」以下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
BillTable
billcount
=
"2"
>
<
bills
>
<
BillItem
billID
=
"010101001"
billname
=
"平整場地"
billunit
=
"m2"
decount
=
"3"
>
<
des
>
<
DesItem
deID
=
"A1-1"
decont
=
"建築場地挖填高度在±30cm之內的找平"
dename
=
"平整場地"
deunit
=
"100m2"
/>
<
DesItem
deID
=
"A1-3"
decont
=
"土方開挖"
dename
=
"人工挖土方 三類土 深度在1.5m內"
deunit
=
"100m3"
/>
<
DesItem
deID
=
"A1-4"
decont
=
"土方開挖"
dename
=
"人工挖土方 四類土 深度在1.5m內"
deunit
=
"100m3"
/>
</
des
>
</
BillItem
>
<
BillItem
billID
=
"010101005"
billname
=
"挖淤泥、流砂"
billunit
=
"m3"
decount
=
"1"
>
<
des
>
<
DesItem
deID
=
"A1-27"
decont
=
"挖淤泥、流砂"
dename
=
"人工挖淤泥流砂"
deunit
=
"100m3"
/>
</
des
>
</
BillItem
>
</
bills
>
</
BillTable
>
|
Debug輸出以下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
This
is
a Test !
" billcount : 2"
" bills [ "
" billID : 010101001"
" billname : 平整場地"
" billunit : m2"
" decount : 3"
" des [ "
" deID : A1-1"
" decont : 建築場地挖填高度在±30cm之內的找平"
" dename : 平整場地"
" deunit : 100m2"
" deID : A1-3"
" decont : 土方開挖"
" dename : 人工挖土方 三類土 深度在1.5m內"
" deunit : 100m3"
" deID : A1-4"
" decont : 土方開挖"
" dename : 人工挖土方 四類土 深度在1.5m內"
" deunit : 100m3"
" des ] "
" billID : 010101005"
" billname : 挖淤泥、流砂"
" billunit : m3"
" decount : 1"
" des [ "
" deID : A1-27"
" decont : 挖淤泥、流砂"
" dename : 人工挖淤泥流砂"
" deunit : 100m3"
" des ] "
" bills ] "
|
接下來要研讀研讀理論方面的內容了。博文中有錯誤之處,歡迎指正!
聲明:本博文爲博主原創,轉載請註明來源,謝謝合做!