您的位置:首页 > Web前端 > JavaScript

QJson解析数组里面的不同对象的值(怎么将数组转为对象)

2017-09-05 17:20 501 查看
工作需要用到QJson

https://github.com/qt-json/qt-json

用的时候发现一个问题 没有新版的toObject()

然后也找了一段时间解决方法,最后OK

使用toMap()代替

要解析内容如下:



#include "emojiui.h"

emojiUI::emojiUI(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
//QString json = readFile(":/new/prefix1/example.json");
QString json = readFile(":/new/prefix1/emoji.json");
if (json.isEmpty()) {
qFatal("Could not read JSON file!");
return ;
}

bool ok;
JsonObject result = QtJson::parse(json, ok).toMap();
if (!ok) {
qFatal("An error occurred during parsing");
return ;
}
qDebug()<<"-----------";
qDebug()<<"-----------";
jsonprintResult(result);

}

emojiUI::~emojiUI()
{

}

//解析内容
void emojiUI::jsonprintResult(const JsonObject &result) {

JsonObject indent = result["categoryMap"].toMap();
//数组处理
JsonArray plugins = indent["People"].toList();
for(int i=0;i<plugins.count();i++)
{
JsonObject temp = plugins.at(i).toMap();
qDebug() << "  -" <<temp["unicode"].toString();
}
}

QString emojiUI::readFile( const QString &filename )
{
QFile f(filename);
if (!f.open(QFile::ReadOnly | QFile::Text)) {
return QString();
} else {
QTextStream in(&f);
return in.readAll();
}
}


还有使用

QJson要留意命名空间问题

要有

using QtJson::JsonObject;
using QtJson::JsonArray;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: