Last active
December 22, 2015 07:08
-
-
Save cckwes/6435552 to your computer and use it in GitHub Desktop.
QJson Qt5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QApplication> | |
#include <QJsonDocument> | |
#include <QJsonObject> | |
#include <QJsonArray> | |
#include <QJsonParseError> | |
#include <QFile> | |
#include <QDebug> | |
struct DeskWallpaper { | |
int width; | |
int height; | |
QString url; | |
int previewWidth; | |
int previewHeight; | |
QString previewUrl; | |
int thumbWidth; | |
int thumbHeight; | |
QString thumbUrl; | |
void printAll() { | |
qDebug() << "Image width: " << width; | |
qDebug() << "Image height: " << height; | |
qDebug() << "Image url: " << url; | |
qDebug() << "Preview width: " << previewWidth; | |
qDebug() << "Preview height: " << previewHeight; | |
qDebug() << "Preview url: " << previewUrl; | |
qDebug() << "Thumb width: " << thumbWidth; | |
qDebug() << "Thumb height: " << thumbHeight; | |
qDebug() << "Thumb url: " << thumbUrl; | |
} | |
}; | |
int main(int argc, char* argv[]) | |
{ | |
QApplication app(argc, argv); | |
QFile jsonFile("random.json"); | |
if (!jsonFile.open(QIODevice::ReadOnly | QIODevice::Text)) | |
{ | |
qDebug() << "Unable to open file, exiting..."; | |
return 1005; | |
} | |
QByteArray jsonData = jsonFile.readAll(); | |
QJsonParseError *err = new QJsonParseError(); | |
QJsonDocument doc = QJsonDocument::fromJson(jsonData, err); | |
if (err->error != 0) | |
qDebug() << err->errorString(); | |
DeskWallpaper wallpaper; | |
if (doc.isNull()) | |
{ | |
qDebug() << "Invalid json document, exiting..."; | |
return 1006; | |
} | |
else if (doc.isObject()) | |
{ | |
//get the jsonObject | |
QJsonObject jObject = doc.object(); | |
//convert the json object to variantmap | |
QVariantMap mainMap = jObject.toVariantMap(); | |
//get the "response" variant map | |
QVariantMap responseMap = mainMap["response"].toMap(); | |
//get the width and height of image | |
wallpaper.width = responseMap["width"].toInt(); | |
wallpaper.height = responseMap["height"].toInt(); | |
//get the image url | |
QVariantMap imageMap = responseMap["image"].toMap(); | |
wallpaper.url = imageMap["url"].toString(); | |
//get the preview image details | |
QVariantMap previewMap = imageMap["preview"].toMap(); | |
wallpaper.previewWidth = previewMap["width"].toInt(); | |
wallpaper.previewHeight = previewMap["height"].toInt(); | |
wallpaper.previewUrl = previewMap["url"].toString(); | |
//get the thumbnail details | |
QVariantMap thumbMap = imageMap["thumb"].toMap(); | |
wallpaper.thumbWidth = thumbMap["width"].toInt(); | |
wallpaper.thumbHeight = thumbMap["height"].toInt(); | |
wallpaper.thumbUrl = thumbMap["url"].toString(); | |
wallpaper.printAll(); | |
} | |
else if (doc.isArray()) | |
{ | |
qDebug() << "Are you sure this is the json from Desktoppr? exiting..."; | |
return 1007; | |
} | |
else if (doc.isEmpty()) | |
{ | |
qDebug() << "Empty json document, exiting..."; | |
return 1008; | |
} | |
else | |
{ | |
qDebug() << "Unable to determine type of JSON, exiting..."; | |
return 1009; | |
} | |
exit(1); | |
return app.exec(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"response":{ | |
"id":60491, | |
"bytes":2042523, | |
"created_at":"2012-06-23T01:31:26Z", | |
"image":{ | |
"url":"http://a.desktopprassets.com/wallpapers/a61593e4a952def82666790770a7070c13726694/neptune.jpg", | |
"thumb":{ | |
"url":"http://a.desktopprassets.com/wallpapers/a61593e4a952def82666790770a7070c13726694/thumb_neptune.jpg", | |
"width":296, | |
"height":185 | |
}, | |
"preview":{ | |
"url":"http://a.desktopprassets.com/wallpapers/a61593e4a952def82666790770a7070c13726694/preview_neptune.jpg", | |
"width":960, | |
"height":720 | |
} | |
}, | |
"height":1200, | |
"width":1600, | |
"review_state":"safe", | |
"uploader":"maybeso", | |
"user_count":7, | |
"palette":[ | |
"0C1219", | |
"4266A1", | |
"606469", | |
"7595BC", | |
"C1C7CD", | |
"C2C7CD" | |
], | |
"url":"https://www.desktoppr.co/wallpapers/60491" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
random.json file is json output get from Desktoppr API's random wallpaper function.
qtjson_qt5.cpp is a cpp file that demo the use of QJsonDocuments and related function is Qt5 to parse the JSON file