Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1063101
  • 博文数量: 264
  • 博客积分: 6005
  • 博客等级: 大校
  • 技术积分: 2798
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-08 20:15
文章分类

全部博文(264)

文章存档

2011年(42)

2010年(213)

2009年(4)

2008年(2)

2007年(3)

分类:

2010-10-15 21:56:18

Converting JSON to QVariant

Converting JSON's data to QVariant instance is really simple:

1 // create a Parser instance
2 QJson::Parser parser;
3 
4 bool ok;
5 
6 // json is a QString containing the data to convert
7 QVariant result = parser.parse (json, &ok);

Suppose you're going to convert this JSON data:

 1 {
 2   "encoding" : "UTF-8",
 3   "plug-ins" : [
 4       "python",
 5       "c++",
 6       "ruby"
 7       ],
 8   "indent" : { "length" : 3, "use_space" : true }
 9 }

The following code would convert the JSON data and parse it:

 1 QJson::Parser parser;
 2 bool ok;
 3 
 4 QVariantMap result = parser.parse (json, &ok).toMap();
 5 if (!ok) {
 6   qFatal("An error occurred during parsing");
 7   exit (1);
 8 }
 9 
10 qDebug() << "encoding:" << result["encoding"].toString();
11 qDebug() << "plugins:";
12 
13 foreach (QVariant plugin, result["plug-ins"].toList()) {
14   qDebug() << "\t-" << plugin.toString();
15 }
16 
17 QVariantMap nestedMap = result["indent"].toMap();
18 qDebug() << "length:" << nestedMap["length"].toInt();
19 qDebug() << "use_space:" << nestedMap["use_space"].toBool();

The output would be:

1 encoding: "UTF-8"
2 plugins:
3   - "python"
4   - "c++"
5   - "ruby"
6 length: 3
7 use_space: true

Converting QVariant to JSON

Converting a QVariant instance into a JSON object is really simple:

 1 QVariantList people;
 2 
 3 QVariantMap bob;
 4 bob.insert("Name", "Bob");
 5 bob.insert("Phonenumber", 123);
 6 
 7 QVariantMap alice;
 8 alice.insert("Name", "Alice");
 9 alice.insert("Phonenumber", 321);
10 
11 people << bob << alice;
12 
13 QJson::Serializer serializer;
14 QByteArray json = serializer.serialize(people);
15 
16 qDebug() << json;

The output will be:

1 "[ { "Name" : "Bob", "Phonenumber" : 123 },
2    { "Name" : "Alice", "Phonenumber" : 321 } ]"

QObjct serialization and vice-versa

It's also possible to serialize a QObject instance to JSON. All the class attributes defined as properties will be serialized. Suppose the declaration of Person class looks like this:

 1 class Person : public QObject
 2 {
 3   Q_OBJECT
 4 
 5   Q_PROPERTY(QString name READ name WRITE setName)
 6   Q_PROPERTY(int phoneNumber READ phoneNumber WRITE setPhoneNumber)
 7   Q_PROPERTY(Gender gender READ gender WRITE setGender)
 8   Q_PROPERTY(QDate dob READ dob WRITE setDob)
 9   Q_ENUMS(Gender)
10 
11 public:
12     Person(QObject* parent = 0);
13     ~Person();
14 
15     QString name() const;
16     void setName(const QString& name);
17 
18     int phoneNumber() const;
19     void setPhoneNumber(const int  phoneNumber);
20 
21     enum Gender {Male, Female};
22     void setGender(Gender gender);
23     Gender gender() const;
24 
25     QDate dob() const;
26     void setDob(const QDate& dob);
27 
28   private:
29     QString m_name;
30     int m_phoneNumber;
31     Gender m_gender;
32     QDate m_dob;
33 };

The following code will serialize an instance of Person to JSON:

 1 Person person;
 2 person.setName("Flavio");
 3 person.setPhoneNumber(123456);
 4 person.setGender(Person::Male);
 5 person.setDob(QDate(1982, 7, 12));
 6 
 7 QVariantMap variant = QObjectHelper::qobject2qvariant(&person);
 8 Serializer serializer;
 9 qDebug() << serializer.serialize( variant);

The generated output will be:

1 { "dob" : "1982-07-12",
2   "gender" : 0,
3   "name" : "Flavio",
4   "phoneNumber" : 123456 }

It's also possible to initialize a QObject using the values stored inside of a QVariantMap. Suppose you have the following JSON data stored into a QString:

1 { "dob" : "1982-07-12",
2   "gender" : 0,
3   "name" : "Flavio",
4   "phoneNumber" : 123456 }

The following code will initialize an already allocated instance of Person using the JSON values:

1 Parser parser;
2 QVariant variant = parser.parse(json);
3 
4 Person person;
5 QObjectHelper::qvariant2qobject(variant.toMap(), &person);

API

The full documentation of QJson can be found .

阅读(4069) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~