如何创建一个QJsonDocument?
How to create a QJsonDocument?
我想创建一个 QJsonDocument
但我找不到将我的数据推送到文档中的方法。在 JsonBuilder::CreateContact
结束时,我的文档似乎是空的。请提供一些帮助 X)。
我尝试使用一些指针并将它们添加到我的文档中,因为我正在考虑破坏我的不同对象(contactObj
、detailObj
、addressObj
、date
) 但是用这种方法我无法构建类型 QJsonValue
(无法访问 QjsonValue::QJsonValue
的 private
成员)所以我错了吗?
JsonBuilder.h
#ifndef JSONBUILDER_H
#define JSONBUILDER_H
#include <QObject>
#include <QJsonDocument>
#include <QMap>
#include <QString>
#include <QList>
class JsonBuilder: public QObject
{
Q_OBJECT
private:
QJsonDocument* document;
public:
JsonBuilder();
~JsonBuilder();
void CreateContact(QMap<QString,QList<QString>> map);
void CreateMessage(QMap<QString,QString> map);
QJsonDocument* getDocument(){ return document; }
};
#endif // JSONBUILDER_H
JsonBuilder.cpp
#include "Jsonbuilder.h"
#include <QPair>
#include <QJsonObject>
#include <QJsonValue>
#include <Global.h>
#include <QDebug>
#include <QMapIterator>
#include <QJsonArray>
JsonBuilder::JsonBuilder(){
document = new QJsonDocument(QJsonArray());
}
void JsonBuilder::CreateContact(QMap<QString, QList<QString>> map){
QJsonObject contactObj;
QJsonObject detailObj;
QJsonObject addressObj;
QJsonObject date;
contactObj.insert(Global::KEY_TYPE_OBJ,QJsonValue("contact"));
QMapIterator<QString, QList<QString>> it(map);
while (it.hasNext()) {
it.next();
qDebug()<< "Key value = "+it.key();
if(it.key() == Global::KEY_FIRST_NAME){
contactObj.insert(Global::KEY_FIRST_NAME,QJsonValue(it.value()[0]));
qDebug()<< "create firstName in Json " + it.value()[0];
}
if(it.key() == Global::KEY_LAST_NAME){
contactObj.insert(Global::KEY_LAST_NAME,QJsonValue(it.value()[0]));
qDebug()<< "create LastName in Json "+ it.value()[0];
}
if(it.key() == Global::KEY_PSEUDO){
QJsonArray Pseudos;
foreach (QString contact, it.value()) {
Pseudos.append(QJsonValue(contact));
qDebug()<< "create Pseudo in Json "+ contact;
}
contactObj.insert(Global::KEY_PSEUDO,QJsonValue(Pseudos));
}
//Detail Object
if(it.key() == Global::KEY_DETAIL_ROLE){
detailObj.insert(Global::KEY_DETAIL_ROLE,QJsonValue(it.value()[0]));
qDebug()<< "create DetailRole in Json "+ it.value()[0];
}
if(it.key() == Global::KEY_DETAIL_ORGANIZATION){
detailObj.insert(Global::KEY_DETAIL_ORGANIZATION,QJsonValue(it.value()[0]));
qDebug()<< "create DetailOrganization in Json "+ it.value()[0];
}
if(it.key() == Global::KEY_DETAIL_DEPARTMENT){
detailObj.insert(Global::KEY_DETAIL_DEPARTMENT,QJsonValue(it.value()[0]));
qDebug()<< "create DetailDepartment in Json "+ it.value()[0];
}
if(it.key() == Global::KEY_DETAIL_FUNCTION){
detailObj.insert(Global::KEY_DETAIL_FUNCTION,QJsonValue(it.value()[0]));
qDebug()<< "create DetailFunction in Json "+ it.value()[0];
}
if(it.key() == Global::KEY_PHONE){
QJsonArray phones;
foreach (QString phone, it.value()) {
phones.append(QJsonValue(phone));
qDebug()<< "create Phones in Json "+phone;
}
contactObj.insert(Global::KEY_PHONE,QJsonValue(phones));
}
if(it.key() == Global::KEY_EMAIL){
QJsonArray emails;
foreach (QString email, it.value()) {
emails.append(QJsonValue(email));
qDebug()<< "create Emails in Json "+email;
}
contactObj.insert(Global::KEY_EMAIL,QJsonValue(emails));
}
//Dates Object
if(it.key() == Global::KEY_BIRTHDAY){
date.insert(Global::KEY_BIRTHDAY,QJsonValue(it.value()[0]));
qDebug()<< "create Birthday in Json "+it.value()[0];
}
if(it.key() == Global::KEY_WEDDING){
foreach (QString dateWedding, it.value()) {
date.insert(Global::KEY_WEDDING, QJsonValue(dateWedding));
qDebug()<< "create wedding in Json "+dateWedding;
}
}
//Address Object
if(it.key() == Global::KEY_STREET){
addressObj.insert(Global::KEY_STREET,QJsonValue(it.value()[0]));
qDebug()<< "create street in Json "+it.value()[0];
}
if(it.key() == Global::KEY_POSTAL_CODE){
addressObj.insert(Global::KEY_POSTAL_CODE,QJsonValue(it.value()[0]));
qDebug()<< "create PostalCode in Json "+it.value()[0];
}
if(it.key() == Global::KEY_CITY){
addressObj.insert(Global::KEY_CITY, QJsonValue(it.value()[0]));
qDebug()<< "create city in Json "+it.value()[0];
}
if(it.key() == Global::KEY_COUNTRY){
addressObj.insert(Global::KEY_COUNTRY, QJsonValue(it.value()[0]));
qDebug()<< "create Country in Json "+it.value()[0];
}
}
contactObj.insert(Global::KEY_DETAIL,QJsonValue(detailObj));
contactObj.insert(Global::KEY_ADDRESS_OBJ,QJsonValue(addressObj));
contactObj.insert(Global::KEY_DATE,QJsonValue(date));
if(document->isArray()){
document->array().append(contactObj);
}
else {
qDebug()<<"JsonDocument not ready for insert";
}
qDebug()<< "Doc in function create : ";
qDebug()<< *document;
}
void JsonBuilder::CreateMessage(QMap<QString, QString> map){
}
JsonBuilder::~JsonBuilder(){
delete document;
}
注意(在文档中)array() 方法的签名:
QJsonArray QJsonDocument::array() const
当你这样使用它时:
document->array().append(contactObj);
您正在将对象附加到 doc 数组的临时副本。
你最好创建一个数组,附加你的对象,然后将数组设置到文档,这样:
QJsonArray array;
array.append(contactObj);
//append every else object you need
document->setArray(array);
而不是
if(document->isArray()){
document->array().append(contactObj);
}
else {
qDebug()<<"JsonDocument not ready for insert";
}
我想创建一个 QJsonDocument
但我找不到将我的数据推送到文档中的方法。在 JsonBuilder::CreateContact
结束时,我的文档似乎是空的。请提供一些帮助 X)。
我尝试使用一些指针并将它们添加到我的文档中,因为我正在考虑破坏我的不同对象(contactObj
、detailObj
、addressObj
、date
) 但是用这种方法我无法构建类型 QJsonValue
(无法访问 QjsonValue::QJsonValue
的 private
成员)所以我错了吗?
JsonBuilder.h
#ifndef JSONBUILDER_H
#define JSONBUILDER_H
#include <QObject>
#include <QJsonDocument>
#include <QMap>
#include <QString>
#include <QList>
class JsonBuilder: public QObject
{
Q_OBJECT
private:
QJsonDocument* document;
public:
JsonBuilder();
~JsonBuilder();
void CreateContact(QMap<QString,QList<QString>> map);
void CreateMessage(QMap<QString,QString> map);
QJsonDocument* getDocument(){ return document; }
};
#endif // JSONBUILDER_H
JsonBuilder.cpp
#include "Jsonbuilder.h"
#include <QPair>
#include <QJsonObject>
#include <QJsonValue>
#include <Global.h>
#include <QDebug>
#include <QMapIterator>
#include <QJsonArray>
JsonBuilder::JsonBuilder(){
document = new QJsonDocument(QJsonArray());
}
void JsonBuilder::CreateContact(QMap<QString, QList<QString>> map){
QJsonObject contactObj;
QJsonObject detailObj;
QJsonObject addressObj;
QJsonObject date;
contactObj.insert(Global::KEY_TYPE_OBJ,QJsonValue("contact"));
QMapIterator<QString, QList<QString>> it(map);
while (it.hasNext()) {
it.next();
qDebug()<< "Key value = "+it.key();
if(it.key() == Global::KEY_FIRST_NAME){
contactObj.insert(Global::KEY_FIRST_NAME,QJsonValue(it.value()[0]));
qDebug()<< "create firstName in Json " + it.value()[0];
}
if(it.key() == Global::KEY_LAST_NAME){
contactObj.insert(Global::KEY_LAST_NAME,QJsonValue(it.value()[0]));
qDebug()<< "create LastName in Json "+ it.value()[0];
}
if(it.key() == Global::KEY_PSEUDO){
QJsonArray Pseudos;
foreach (QString contact, it.value()) {
Pseudos.append(QJsonValue(contact));
qDebug()<< "create Pseudo in Json "+ contact;
}
contactObj.insert(Global::KEY_PSEUDO,QJsonValue(Pseudos));
}
//Detail Object
if(it.key() == Global::KEY_DETAIL_ROLE){
detailObj.insert(Global::KEY_DETAIL_ROLE,QJsonValue(it.value()[0]));
qDebug()<< "create DetailRole in Json "+ it.value()[0];
}
if(it.key() == Global::KEY_DETAIL_ORGANIZATION){
detailObj.insert(Global::KEY_DETAIL_ORGANIZATION,QJsonValue(it.value()[0]));
qDebug()<< "create DetailOrganization in Json "+ it.value()[0];
}
if(it.key() == Global::KEY_DETAIL_DEPARTMENT){
detailObj.insert(Global::KEY_DETAIL_DEPARTMENT,QJsonValue(it.value()[0]));
qDebug()<< "create DetailDepartment in Json "+ it.value()[0];
}
if(it.key() == Global::KEY_DETAIL_FUNCTION){
detailObj.insert(Global::KEY_DETAIL_FUNCTION,QJsonValue(it.value()[0]));
qDebug()<< "create DetailFunction in Json "+ it.value()[0];
}
if(it.key() == Global::KEY_PHONE){
QJsonArray phones;
foreach (QString phone, it.value()) {
phones.append(QJsonValue(phone));
qDebug()<< "create Phones in Json "+phone;
}
contactObj.insert(Global::KEY_PHONE,QJsonValue(phones));
}
if(it.key() == Global::KEY_EMAIL){
QJsonArray emails;
foreach (QString email, it.value()) {
emails.append(QJsonValue(email));
qDebug()<< "create Emails in Json "+email;
}
contactObj.insert(Global::KEY_EMAIL,QJsonValue(emails));
}
//Dates Object
if(it.key() == Global::KEY_BIRTHDAY){
date.insert(Global::KEY_BIRTHDAY,QJsonValue(it.value()[0]));
qDebug()<< "create Birthday in Json "+it.value()[0];
}
if(it.key() == Global::KEY_WEDDING){
foreach (QString dateWedding, it.value()) {
date.insert(Global::KEY_WEDDING, QJsonValue(dateWedding));
qDebug()<< "create wedding in Json "+dateWedding;
}
}
//Address Object
if(it.key() == Global::KEY_STREET){
addressObj.insert(Global::KEY_STREET,QJsonValue(it.value()[0]));
qDebug()<< "create street in Json "+it.value()[0];
}
if(it.key() == Global::KEY_POSTAL_CODE){
addressObj.insert(Global::KEY_POSTAL_CODE,QJsonValue(it.value()[0]));
qDebug()<< "create PostalCode in Json "+it.value()[0];
}
if(it.key() == Global::KEY_CITY){
addressObj.insert(Global::KEY_CITY, QJsonValue(it.value()[0]));
qDebug()<< "create city in Json "+it.value()[0];
}
if(it.key() == Global::KEY_COUNTRY){
addressObj.insert(Global::KEY_COUNTRY, QJsonValue(it.value()[0]));
qDebug()<< "create Country in Json "+it.value()[0];
}
}
contactObj.insert(Global::KEY_DETAIL,QJsonValue(detailObj));
contactObj.insert(Global::KEY_ADDRESS_OBJ,QJsonValue(addressObj));
contactObj.insert(Global::KEY_DATE,QJsonValue(date));
if(document->isArray()){
document->array().append(contactObj);
}
else {
qDebug()<<"JsonDocument not ready for insert";
}
qDebug()<< "Doc in function create : ";
qDebug()<< *document;
}
void JsonBuilder::CreateMessage(QMap<QString, QString> map){
}
JsonBuilder::~JsonBuilder(){
delete document;
}
注意(在文档中)array() 方法的签名:
QJsonArray QJsonDocument::array() const
当你这样使用它时:
document->array().append(contactObj);
您正在将对象附加到 doc 数组的临时副本。
你最好创建一个数组,附加你的对象,然后将数组设置到文档,这样:
QJsonArray array;
array.append(contactObj);
//append every else object you need
document->setArray(array);
而不是
if(document->isArray()){
document->array().append(contactObj);
}
else {
qDebug()<<"JsonDocument not ready for insert";
}