对于这样一段json
{
"name": "布衣食",
"gender": "Male",
"age": 25,
"attribute": {
"skill": "Programming",
"languages": [ "C", "C++", "Java" ],
"frameworks": [ "Qt", "Android" ]
}
}
我希望把它转换为一个Programmer对象,Programmer的声明如下
Programmer.h
#ifndef PROGRAMMER_H
#define PROGRAMMER_H
#include <QString>
#include <QList>
class Programmer
{
public:
class Attribute
{
public:
Attribute(const QString &skill, const QList<QString> &languages, const QList<QString> &frameworks);
const QString &getSkill() const;
const QList<QString> &getLanguages() const;
const QList<QString> &getFrameworks() const;
private:
QString skill;
QList<QString> languages;
QList<QString> frameworks;
};
Programmer(const QString &name, const QString &gender, int age, Attribute attribute);
const QString &getName() const;
const QString &getGender() const;
int getAge() const;
const Attribute &getAttribute() const;
QString toJson() const;
static Programmer fromJson(const QString &json);
private:
QString name;
QString gender;
int age;
Attribute attribute;
};
#endif // PROGRAMMER_H
在Java中,使用第三方库GSON就可以方便地实现对象和json之间的转换;而C++没有反射机制,所以没有类似的库。
于是,我只好自行编写转换代码,如下
将json转换为Programmer对象
Programmer Programmer::fromJson(const QString &json)
{
QJsonParseError error;
const QJsonDocument &document = QJsonDocument::fromJson(json.toUtf8(), &error);
if (QJsonParseError::NoError == error.error)
{
const QJsonObject &programmerObject = document.object();
const QString &name = programmerObject.value(QStringLiteral("name")).toString();
const QString &gender = programmerObject.value(QStringLiteral("gender")).toString();
int age = programmerObject.value(QStringLiteral("age")).toInt();
const QJsonObject &attributeObject = programmerObject.value(QStringLiteral("attribute")).toObject();
const QString &skill = attributeObject.value(QStringLiteral("skill")).toString();
const QJsonArray &languagesArray = attributeObject.value(QStringLiteral("languages")).toArray();
QList<QString> languages;
for (const QJsonValue &value : languagesArray)
{
languages.append(value.toString());
}
const QJsonArray &frameworksArray = attributeObject.value(QStringLiteral("frameworks")).toArray();
QList<QString> frameworks;
for (const QJsonValue &value : frameworksArray)
{
frameworks.append(value.toString());
}
return Programmer(name, gender, age, Attribute(skill, languages, frameworks));
}
throw error.errorString();
}
将Programmer对象转换为json
QString Programmer::toJson() const
{
QJsonObject programmerObject;
programmerObject.insert(QStringLiteral("name"), name);
programmerObject.insert(QStringLiteral("gender"), gender);
programmerObject.insert(QStringLiteral("age"), age);
QJsonObject attributeObject;
attributeObject.insert(QStringLiteral("skill"), attribute.getSkill());
QJsonArray languagesArray;
for (const QString &s : attribute.getLanguages())
{
languagesArray.append(s);
}
attributeObject.insert(QStringLiteral("languages"), languagesArray);
QJsonArray frameworksArray;
for (const QString &s : attribute.getFrameworks())
{
frameworksArray.append(s);
}
attributeObject.insert(QStringLiteral("frameworks"), frameworksArray);
programmerObject.insert(QStringLiteral("attribute"), attributeObject);
return QJsonDocument(programmerObject).toJson();
}
编写这样的代码,枯燥费时,但我目前没有更好的解决方案。