码迷,mamicode.com
首页 > Web开发 > 详细

QtJson用法

时间:2015-11-30 00:44:29      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:

序列化:
 1 QtJson::JsonObject friend1, friend2, friend3;
 2 friend1["id"] = 1;
 3 friend1["name"] = "Mackenzie Hamphrey";
 4 
 5 friend2["id"] = 2;
 6 friend2["name"] = "Melanie Molligan";
 7 
 8 friend3["id"] = 3;
 9 friend3["name"] = "Sydney Calhoun";
10 
11 QtJson::JsonArray friends;
12 friends.append(friend1);
13 friends.append(friend2);
14 friends.append(friend3);
15 
16 QtJson::JsonObject obj;
17 obj["friends"] = friends;

反序列化

 1 #include "json.h"
 2 
 3 bool ok;
 4 // json is a QString containing the JSON data
 5 QtJson::JsonObject result = QtJson::parse(json, ok).toMap();
 6 
 7 if(!ok) {
 8   qFatal("An error occurred during parsing");
 9 
10 qDebug() << "encoding:" << result["encoding"].toString();
11 qDebug() << "plugins:";
12 
13 foreach(QVariant plugin, result["plug-ins"].toList()) {
14     qDebug() << "  -" << plugin.toString();
15 }
16 
17 QtJson::JsonObject nested = result["indent"].toMap();
18 qDebug() << "length:" << nested["length"].toInt();
19 qDebug() << "use_space:" << nested["use_space"].toBool();

 

QtJson用法

标签:

原文地址:http://www.cnblogs.com/huaminghu/p/5005982.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!