qt5 解析Json文件
2015-12-06 11:00
441 查看
/* test.json */ { "appDesc": { "description": "SomeDescription", "message": "SomeMessage" }, "appName": { "description": "Home", "message": "Welcome", "imp":["awesome","best","good"] } } void readJson() { QString val; QFile file; file.setFileName("test.json"); file.open(QIODevice::ReadOnly | QIODevice::Text); val = file.readAll(); file.close(); qWarning() << val; QJsonDocument d = QJsonDocument::fromJson(val.toUtf8()); QJsonObject sett2 = d.object(); QJsonValue value = sett2.value(QString("appName")); qWarning() << value; QJsonObject item = value.toObject(); qWarning() << tr("QJsonObject of description: ") << item; /* incase of string value get value and convert into string*/ qWarning() << tr("QJsonObject[appName] of description: ") << item["description"]; QJsonValue subobj = item["description"]; qWarning() << subobj.toString(); /* incase of array get array and convert into string*/ qWarning() << tr("QJsonObject[appName] of value: ") << item["imp"]; QJsonArray test = item["imp"].toArray(); qWarning() << test[1].toString(); }
http://stackoverflow.com/questions/15893040/how-to-create-read-write-json-files-in-qt5
摘于上面的链接,大部分已经能用了。
我来说下其他情况:
{"file":"book.png","frames":{
"v1":{"x":1,"y":91,"w":68,"h":87,"offX":0,"offY":0,"sourceW":68,"sourceH":87},
"v2":{"x":1,"y":1,"w":68,"h":88,"offX":0,"offY":0,"sourceW":68,"sourceH":88},
"v3":{"x":209,"y":1,"w":66,"h":87,"offX":0,"offY":0,"sourceW":66,"sourceH":87},
"v4":{"x":71,"y":1,"w":67,"h":88,"offX":0,"offY":0,"sourceW":67,"sourceH":88},
"v5":{"x":71,"y":91,"w":67,"h":88,"offX":0,"offY":0,"sourceW":67,"sourceH":88},
"v6":{"x":140,"y":1,"w":67,"h":87,"offX":0,"offY":0,"sourceW":67,"sourceH":87},
"v7":{"x":140,"y":90,"w":67,"h":87,"offX":0,"offY":0,"sourceW":67,"sourceH":87}}}
像这样的json,想要得到frames里所有的内容,因为它不是一个数组,所以要用迭代器来访问,类似这样的代码:
bool MainWindow::parseJsonFile(){
QString val;
QFile file;
file.setFileName("test.json");
file.open(QIODevice::ReadOnly | QIODevice::Text);
val = file.readAll();
file.close();
qWarning() << val;
QJsonDocument d = QJsonDocument::fromJson(val.toUtf8());
QJsonObject rootObject = d.object();
QJsonValue pngNameJsonValue = rootObject.value(QString("file"));
qWarning() << pngNameJsonValue.toString();
QJsonValue framesJsonValue = rootObject.value(QString("frames"));
qWarning() << framesJsonValue;
QStringList imgNameList = framesJsonValue.toObject().keys();
QJsonObject frameObject = framesJsonValue.toObject();
int index = 0;
for(auto beginItr = frameObject.begin(); beginItr != frameObject.end(); ++beginItr){
QJsonValue eachImageJsonValue = *beginItr;
QJsonObject eachImageJsonObject = eachImageJsonValue.toObject();
//eachImageJsonObject["x"], eachImageJsonObject["y"] ...
}
return true;
}
还有QJsonValue里用.keys()得到所有的key,然后就可以通过["key"] 来访问了。
http://www.waitingfy.com/archives/1775
相关文章推荐
- MySQL的安装、编译Qt驱动和测试
- MySQL的安装、编译Qt驱动和测试
- PyQt5教程(五)——对话框
- Qt 设置鼠标
- Qt 打开保存文件对话框
- Qt 设置程序图标
- Qt 创建上下文菜单
- Qt如何实现多窗口调用
- Opencv--Qt5编译与配置
- Windows下 Qt5.1 not load The QtplatForm plugin "windows"
- QT5 下udp 编程实例
- QT线程(一):线程类 http://blog.csdn.net/calm_agan/article/details/6300709
- QtPropertyBrowser简介
- Qt udp
- Qt5 UDP开发
- 关于QT中对窗口中某个widget建类
- U1077:"C:\Qt5.4.2\5.4\msvc2013_64_opengl\bin\moc.exe":返回代码“0x1”
- Qt The Event System
- qt+opencv3.0配置详解
- 简述QTP自动化测试框架