Skip to content

Instantly share code, notes, and snippets.

@anak10thn
Forked from cckwes/qjson_qt5.cpp
Created October 10, 2013 17:42
Show Gist options
  • Save anak10thn/6922481 to your computer and use it in GitHub Desktop.
Save anak10thn/6922481 to your computer and use it in GitHub Desktop.
#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();
}
{
"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