April 2018
Beginner
714 pages
18h 21m
English
Let's extend the PlayerInfoJSON class and equip it with a playerInfoFromJson() method:
PlayerInfo PlayerInfoJson::playerInfoFromJson(const QByteArray &ba) {
QJsonDocument doc = QJsonDocument::fromJson(ba);
if(!doc.isArray()) {
return PlayerInfo();
}
QJsonArray array = doc.array();
PlayerInfo pinfo;
for(const QJsonValue &value: array) {
pinfo.players << playerFromJson(value.toObject());
}
return pinfo;
}
First, we read the document and check whether it is valid and holds the expected array. Upon failure, an empty structure is returned; otherwise, we iterate over the received array and convert each of its elements to an object. Similar to the serialization example, we create a helper function for ...
Read now
Unlock full access