下载链接 http://qjson.sourceforge.net/build/
1、解压QJson-master压缩包,新建一个QJson目录,将QJson-master中的include 和src目录拷贝到QJson目录下。
然后,再QJson目录下创建一个qjson.pro的项目文件,在文件中编写如下内容:
TARGET = qjson
TEMPLATE = lib
CONFIG += shared
CONFIG += dll
#QMAKE_CXXFLAGS += -fexceptions
CONFIG += exceptions
DEFINES += QJSON_MAKEDLL
#Mingw
win32-g++{
BINDIR = ../win32-gcc-bin
}
#Vs2010
#win32-msvc2010{
# BINDIR = ../../vs2010-bin
#}
##arm-linux
#linux-arm-g++{
# BINDIR = ../arm-linux-gcc-bin
#
# DEFINES += __NO_PRECOMPILED_HEADER__
# DEFINES += _TTY_POSIX_
#}
#CONFIG(debug, debug|release){
# DESTDIR = $$BINDIR/debug
#}else{
# DESTDIR = $$BINDIR/release
#}
SOURCES += \
HEADERS +=\
然后使用Qt4.8.3打开项目工程文件qjson.pro,在项目树上,添加现有文件,将include和src目录下的文件添加到qjson项目中,构建工程,正常可生成qjson.dll库文件。
1、新建一个Qt控制台项目 — testQjson ,在pro文件中链接qjson动态库。将qjson的include目录拷贝到当前测试项目目录下。可以将头文件添加到项目中,也可以不用添加。
#-------------------------------------------------
#
# Project created by QtCreator 2020-05-15T08:29:29
#
#-------------------------------------------------
QT += core
QT -= gui
TARGET = testQjson
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
DEFINES += QJSON_MAKEDLL
SOURCES += main.cpp \
include/person.cpp
LIBS += -L$$PWD/release/bin/ -lqjson
INCLUDEPATH += $$PWD/release/bin
DEPENDPATH += $$PWD/release/bin
HEADERS += \
include/FlexLexer.h \
include/json_parser.hh \
include/json_scanner.h \
include/location.hh \
include/parser.h \
include/parser_p.h \
include/parserrunnable.h \
include/position.hh \
include/qjson_debug.h \
include/qjson_export.h \
include/qobjecthelper.h \
include/serializer.h \
include/serializerrunnable.h \
include/stack.hh \
include/person.h
然后我们添加一个cpp类如下:
// person.h
#ifndef PERSON_H
#define PERSON_H
#include
#include
#include
#include
#include
class Person : public QObject
{
Q_OBJECT
Q_PROPERTY(QString name READ name WRITE setName)
Q_PROPERTY(int phoneNumber READ phoneNumber WRITE setPhoneNumber)
Q_PROPERTY(Gender gender READ gender WRITE setGender)
Q_PROPERTY(QDate dob READ dob WRITE setDob)
Q_ENUMS(Gender)
public:
Person(QObject* parent = 0);
~Person();
QString name() const;
void setName(const QString& name);
int phoneNumber() const;
void setPhoneNumber(const int phoneNumber);
enum Gender {Male, Female};
void setGender(Gender gender);
Gender gender() const;
QDate dob() const;
void setDob(const QDate& dob);
public:
QString m_name;
int m_phoneNumber;
Gender m_gender;
QDate m_dob;
};
#endif // PERSON_H
// person.cpp
#include "person.h"
Person::Person(QObject *parent) : QObject(parent)
{
}
Person::~Person(){}
QString Person::name() const
{
return m_name;
}
void Person::setName(const QString &name)
{
m_name = name;
}
int Person::phoneNumber() const
{
return m_phoneNumber;
}
void Person::setPhoneNumber(const int phoneNumber)
{
m_phoneNumber = phoneNumber;
}
void Person::setGender(Person::Gender gender)
{
m_gender = gender;
}
Person::Gender Person::gender() const
{
return m_gender;
}
QDate Person::dob() const
{
return m_dob;
}
void Person::setDob(const QDate &dob)
{
m_dob = dob;
}
在main.cpp 文件中,我们使用QJson中几种不同的方式来生成Json字符串和解析Json字符串。
#include
#include "../../include/parser.h"
#include "../../include/qobjecthelper.h"
#include "../../include/serializer.h"
#include "../../include/person.h"
using namespace QJson;
typedef struct {
QString name;
int age;
}student_t;
Q_DECLARE_METATYPE(student_t)
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
/**一 、[将特定类中的成员变量保存在json字符串中],个人感觉其优点也是其缺点,不够灵活***********************/
// 组成json格式 ==》 class类型
Person person;
person.setName("Flavio");
person.setPhoneNumber(123456);
person.setGender(Person::Male);
person.setDob(QDate(1982, 7, 12));
QVariantMap variant = QJson::QObjectHelper::qobject2qvariant(&person);
QJson::Serializer serializer;
QByteArray json_string = serializer.serialize( variant);
qDebug() << json_string;
// "{ "dob" : "1982-07-12", "gender" : 0, "name" : "Flavio", "phoneNumber" : 123456 }"
// json字符串解析
QJson::Parser parser;
bool ok;
QVariantMap result = parser.parse(json_string, &ok).toMap();
if (!ok) {
qFatal("An error occurred during parsing");
exit (1);
}
qDebug() << "dob:" << result["dob"].toString()
<< " gender:" << result["gender"].toInt()
<< " name:" << result["name"].toString()
<< " phoneNumber:" << result["phoneNumber"].toInt();
/**二、简单的json字符串生成*************************************/
// json字符串生成==》 自定义类型1:
student_t student = {"zhangshan",20};
QVariantMap v_st;
v_st.insert("name",student.name);
v_st.insert("age",student.age);
qDebug() << serializer.serialize( v_st);
/*****************************************************************************************/
/**三、复杂的json字符串生成****************************************************/
// parser complex json string
/*
* QString json("{"
"\"encoding\" : \"UTF-8\","
"\"plug-ins\" : [\"python\",\"c++\",\"ruby\"],"
"\"types\":[{\"A\":1,\"B\":5},{\"A\":2,\"B\":6}],"
"\"indent\" : { \"length\" : 3, \"use_space\" : true }"
"}");
*/
/// json字符串生成
QVariantMap js_t;
js_t.insert("encoding","UTF-8");
/// 添加json对象
QVariantMap indent_map;
indent_map.insert("length",3);
indent_map.insert("use_space",true);
js_t.insert("indent",indent_map);
/// 添加json数组对象1
QVariantList plug_ins_list;
plug_ins_list << "python" << "c++" << "ruby";
js_t.insert("plug-ins",plug_ins_list);
/// 添加json数组对象1
QVariantList type_list;
QVariantMap map1;
map1.insert("A",1);
map1.insert("B",5);
QVariantMap map2;
map2.insert("A",2);
map2.insert("B",6);
type_list << map1 << map2;
js_t.insert("types",type_list);
qDebug() << "***************************************************";
QString json = serializer.serialize( js_t);
qDebug() << json;
qDebug() << "***************************************************";
/// 此json字符串的解析
result = parser.parse(json.toUtf8(), &ok).toMap();
if (!ok) {
qFatal("An error occurred during parsing");
exit (1);
}
qDebug() << "json:" << result;
/* 通过这里的打印结果,其实可以逆向验证我们使用QJson组成对象的方法是否正确
*
QMap(
("encoding", QVariant(QString, "UTF-8") )
( "indent" , QVariant(QVariantMap, QMap(("length", QVariant(qulonglong, 3) )
( "use_space" , QVariant(bool, true) ) ) ) )
( "plug-ins" , QVariant(QVariantList, (QVariant(QString, "python") , QVariant(QString, "c++") , QVariant(QString, "ruby") ) ) )
( "types" ,
QVariant(QVariantList,
(
QVariant(QVariantMap, QMap(("A", QVariant(qulonglong, 1) ) ( "B" , QVariant(qulonglong, 5) ) ) ) ,
QVariant(QVariantMap, QMap(("A", QVariant(qulonglong, 2) ) ( "B" , QVariant(qulonglong, 6) ) ) ) )
)
)
)
*/
qDebug() << "encoding:" << result["encoding"].toString();
qDebug() << "plugins:";
foreach (QVariant plugin, result["plug-ins"].toList()) {
qDebug() << "\t-" << plugin.toString();
}
qDebug() << result["types"];
foreach (QVariant type, result["types"].toList()) {
qDebug() << "type:" << type;
QVariantMap ret = type.toMap();
qDebug() << "A:" << ret["A"].toInt() << " B:" << ret["B"].toInt();
}
QVariantMap nestedMap = result["indent"].toMap();
qDebug() << "length:" << nestedMap["length"].toInt();
qDebug() << "use_space:" << nestedMap["use_space"].toBool();
return a.exec();
}
打印输出结果如下:
{ "dob" : "1982-07-12", "gender" : 0, "name" : "Flavio", "phoneNumber" : 123456 }"
dob: "1982-07-12" gender: 0 name: "Flavio" phoneNumber: 123456
"{ "age" : 20, "name" : "zhangshan" }"
***************************************************
"{ "encoding" : "UTF-8", "indent" : { "length" : 3, "use_space" : true }, "plug-ins" : [ "python", "c++", "ruby" ], "types" : [ { "A" : 1, "B" : 5 }, { "A" : 2, "B" : 6 } ] }"
***************************************************
json: QMap(("encoding", QVariant(QString, "UTF-8") ) ( "indent" , QVariant(QVariantMap, QMap(("length", QVariant(qulonglong, 3) ) ( "use_space" , QVariant(bool, true) ) ) ) ) ( "plug-ins" , QVariant(QVariantList, (QVariant(QString, "python") , QVariant(QString, "c++") , QVariant(QString, "ruby") ) ) ) ( "types" , QVariant(QVariantList, (QVariant(QVariantMap, QMap(("A", QVariant(qulonglong, 1) ) ( "B" , QVariant(qulonglong, 5) ) ) ) , QVariant(QVariantMap, QMap(("A", QVariant(qulonglong, 2) ) ( "B" , QVariant(qulonglong, 6) ) ) ) ) ) ) )
encoding: "UTF-8"
plugins:
- "python"
- "c++"
- "ruby"
QVariant(QVariantList, (QVariant(QVariantMap, QMap(("A", QVariant(qulonglong, 1) ) ( "B" , QVariant(qulonglong, 5) ) ) ) , QVariant(QVariantMap, QMap(("A", QVariant(qulonglong, 2) ) ( "B" , QVariant(qulonglong, 6) ) ) ) ) )
type: QVariant(QVariantMap, QMap(("A", QVariant(qulonglong, 1) ) ( "B" , QVariant(qulonglong, 5) ) ) )
A: 1 B: 5
type: QVariant(QVariantMap, QMap(("A", QVariant(qulonglong, 2) ) ( "B" , QVariant(qulonglong, 6) ) ) )
A: 2 B: 6
length: 3
use_space: true
因为比较简单,这里只做分享和记录,不过多赘述。需要相关资源的,可以留言或者私我。下一篇,我们来记录下jsoncpp的编译和使用技巧。