转自:http://blog.csdn.net/chencunheng/article/details/42417781
学习
qjson的编译和使用原创 2015年01月05日 08:43:22 标签:Qt Qjson 1133
QJson配置
对于JSON 的解析,我们可以使用 QJson 这个第三方库。QJson可以将 JSON 数据转换为 QVariant对象,将 JSON 数组转换成 QVariantList对象,将 JSON 对象转换成 QVariantMap 对象。QJson 链接库的 pro文件已经写好,内容如下:
TEMPLATE = lib
TARGET =
DEPENDPATH += .
INCLUDEPATH += .
DEFINES += QJSON_MAKEDLL
# Input
HEADERS += FlexLexer.h \
json_parser.hh \
json_scanner.h \
location.hh \
parser.h \
parser_p.h \
parserrunnable.h \
position.hh \
qjson_debug.h \
qjson_export.h \
qobjecthelper.h \
serializer.h \
serializerrunnable.h \
stack.hh
SOURCES += json_parser.cc \
json_scanner.cc \
json_scanner.cpp \
parser.cpp \
parserrunnable.cpp \
qobjecthelper.cpp \
serializer.cpp \
serializerrunnable.cpp
编译好后就可以拿来使用。
在使用前,需要配置使用环境。
1、在本地编译出QJson的dll,放在你的输出目录。
2、在.pro文件中加入如下代码:
INCLUDEPATH+= qjson(头文件目录)
LIBS+= -L(lib文件目录) -lqjson
环境就配好了。
实例类Person的头文件如下:
#pragma once
#include <QtCore/QCoreApplication>
class Person : public QObject
{
Q_OBJECT
Q_PROPERTY(QString name READ name WRITE setName)
Q_PROPERTY(QString phone READ phone WRITE setPhone)
Q_PROPERTY(QString sex READ sex WRITE setSex)
Q_PROPERTY(int age READ age WRITE setAge)
Q_PROPERTY(QString addr READ name WRITE setAddr)
Q_PROPERTY(QString eamil READ phone WRITE setEmail)
Q_PROPERTY(QString work READ sex WRITE setWork)
Q_PROPERTY(QString country READ age WRITE setCountry)
Q_PROPERTY(double weight READ weight WRITE setWeight)
Q_PROPERTY(double height READ height WRITE setHeight)
//Q_ENUMS(Gender)
public:
Person(QObject* parent = 0);
~Person();
QString name() const;
void setName(const QString& name);
QString phone() const;
void setPhone(const QString phone);
QString addr() const;
void setAddr(const QString& addr);
QString email() const;
void setEmail(const QString email);
QString sex() const;
void setSex(const QString& sex);
QString work() const;
void setWork(const QString work);
QString country() const;
void setCountry(const QString& country);
double weight() const;
void setWeight(const double weight);
double height() const;
void setHeight(const double& height);
int age() const;
void setAge(const int age);
public:
QString _name;
QString _addr;
QString _email;
QString _phone;
QString _sex;
double _height;
double _weight;
QString _work;
int _age;
QString _country;
};
实例类Person的实现文件
#include "person.h"
Person::Person(QObject* parent )
{
_name = "xxx";
_addr= "xxx";
_email= "xxx";
_phone= "xxx";
_sex= "X";
_height= 170;
_weight= 60;
_work= "xxxx";
_age= 28;
_country= "xxxx";
}
Person::~Person()
{
}
QString Person::name() const
{
return this->_name;
}
void Person::setName(const QString& name)
{
this->_name = name;
}
QString Person::phone() const
{
return this->_phone;
}
void Person::setPhone(const QString phone)
{
this->_phone = phone;
}
QString Person::addr() const
{
return this->_addr;
}
void Person::setAddr(const QString& addr)
{
this->_addr = addr;
}
QString Person::email() const
{
return this->_email;
}
void Person::setEmail(const QString email)
{
this->_email = email;
}
QString Person::sex() const
{
return this->_sex;
}
void Person::setSex(const QString& sex)
{
this->_sex = sex;
}
QString Person::work() const
{
return this->_work;
}
void Person::setWork(const QString work)
{
this->_work = work;
}
QString Person::country() const
{
return this->_country;
}
void Person::setCountry(const QString& country)
{
this->_country = country;
}
double Person::weight() const
{
return this->_weight;
}
void Person::setWeight(const double weight)
{
this->_weight = weight;
}
double Person::height() const
{
return this->_height;
}
void Person::setHeight(const double& height)
{
this->_height = height;
}
int Person::age() const
{
return this->_age;
}
void Person::setAge(const int age)
{
this->_age = age;
}
main文件 如下
#include <QtCore/QCoreApplication>
//#include <qstring.h>
#include <QTextCodec>
//#include <qstringlist.h>
#include <iostream>
//#include <qdatetime.h>
//#include <QLibrary>
#include <qvariant.h>
#include <include\qjson\parser.h>
#include<include\qjson\qobjecthelper.h>
#include<include\qjson\serializer.h>
#include <qdebug.h>
#include "person.h"
#include <qdatetime.h>
#include<sys\timeb.h>
using namespace std;
//#pragma comment(lib,"E:/MyWork/TestQjson/TestQjson/lib/qjson.lib")
qint64 PERSON_COUNT = 10000;
int main(int argc, char *argv[])
{
//QCoreApplication a(argc, argv);
QTextCodec::setCodecForTr(QTextCodec::codecForName("GB2312"));
QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GB2312"));
QTextCodec::setCodecForLocale(QTextCodec::codecForName("GB2312"));
std::cout<<"*************************TestQJson****************************\n"<<endl;
//qDebug() <<" ------------book-----------";
//QJson::Parser parser;
//bool ok;
//QString bookQuery = "[{\"bookName\":\"神雕侠侣\", \"author\":\"金庸\", \"price\":\"40元\"},{\"bookName\":\"武林外史\", \"author\":\"古龙\", \"price\":\"80元\"}]";
//QVariant result = parser.parse(bookQuery.toUtf8()/*.toAscii()*/, &ok);
//if(ok)
// {
// cout<<"["<<endl;
// QVariantList bookList = result.toList();
//
//
// foreach (QVariant book, bookList)
// {
//
// QVariantMap bookMap = book.toMap();
// //std::cout<<bookMap.value("bookName").toString().toStdString();
// //cout<<endl;
//
// qDebug() << "{" <<"\n"
// << "bookName:"+bookMap["bookName"].toString() <<"\n"
// <<"author:"+ bookMap["author"].toString() <<"\n"
// <<"price:"+ bookMap["price"].toString() <<"\n"
// << "}";
// }
// // cout<<endl;
// cout<<"]"<<endl;
//}else
//qDebug()<<"error occuring";
QString bookQuery
//qDebug() <<" ------------school-----------";
//
//QString schoolQuery = "{\"schoolName\":\"烟台大学\", \"schoolMaster\":\"谢晓峰\",\"teachers\":[{\"name\":\"燕十三\",\"age\":60},{\"name\":\"丁鹏\",\"age\":50}],\"students\":[{\"name\":\"张三\",\"age\":20},{\"name\":\"李四\",\"age\":20}]}";
//QVariant result2 = parser.parse(schoolQuery.toUtf8(),&ok);
//if(ok)
//{
// qDebug()<<"{";
// QVariantMap schoolMap = result2.toMap();
// qDebug()<<"学校名字:"+schoolMap["schoolName"].toString();
// qDebug()<<"校长:"+schoolMap["schoolMaster"].toString();
// qDebug()<<"教师:[";
// QVariantList teacherList = schoolMap["teachers"].toList();
// foreach(QVariant teacher,teacherList)
// {
// QVariantMap teacherMap = teacher.toMap();
// qDebug()<<"{";
// qDebug()<<"name:"+teacherMap["name"].toString();
// qDebug()<<"age:"+teacherMap["age"].toString();
// qDebug()<<"}";
// }
// qDebug()<<"]";
// qDebug()<<"学生:[";
// QVariantList studentsList = schoolMap["students"].toList();
// foreach(QVariant student ,studentsList)
// {
// QVariantMap studentMap = student.toMap();
// qDebug()<<"{";
// qDebug()<<"name:"+studentMap["name"].toString();
// qDebug()<<"age:"+ studentMap["age"].toString();
// qDebug()<<"}";
// }
// qDebug()<<"]";
// qDebug()<<"}";
//}
/*QVariantMap variantMap;;
variantMap.insert("name","zhang");
variantMap.insert("sex","boy");
QString name = "zhangsan";
qDebug() << serializer.serialize( variantMap);*/
//Person person;
//QVariantMap personMap = QJson::QObjectHelper::qobject2qvariant(&person);
QJson::QObjectHelper::qvariant2qobject(personMap,&person);
//qDebug() << serializer.serialize(personMap);
QJson::Serializer serializer;
QJson::Parser parser;
bool ok;
QTime time1 ;
time1.start();
Person person ;
QVariantList personList;
for(int i = 0;i < PERSON_COUNT;i++ )
{
QVariantMap& personMap = QJson::QObjectHelper::qobject2qvariant(&person);
personList.append(personMap);
}
QByteArray qjson = serializer.serialize(personList,&ok);
if(ok)
{
int aaaa =time1.elapsed();
//qDebug() <<qjson;
qDebug() << QString("生成%1个对象的QJson时间为:").arg(PERSON_COUNT)
<< aaaa<<"毫秒"<<"\n";
}
else qDebug()<<"error occuring";
QTime time2 ;
time2.start();
//QString qjsonStr = QString(qjson);
QVariant result = parser.parse(qjson/*.toAscii()*/, &ok);
if(ok)
{
QVariantList jsonList = result.toList();
Person person;
foreach (QVariant var, jsonList)
{
QVariantMap personMap = var.toMap();
// person.setName("不是xxxxx");
QJson::QObjectHelper::qvariant2qobject(personMap,&person);
// qDebug()<<person.name();
}
qDebug() << QString("解析含有%1个对象的QJson耗费的时间为:").arg(PERSON_COUNT)
<<time2.elapsed()<<"毫秒"<<"\n";
}
else qDebug()<<"error occuring";
qDebug() << QString("一共耗费的时间为:")<<time1.elapsed()<<"毫秒"<<"\n";;
system("pause");
return 0;
// return a.exec();
}