Using QJson for read and display A list into QStandardItemModel
This more or less what Qt do with ui which xml file convert to .h. What I'm trying to do is get List from Json file
_____________JsonFile_____________
Code:
{
"common_message": {
"Id_message": "NA"
"Lenght" : "NA",
"bit" : "NA",[B][/B]
etc
}
}
_________________________________
I want to display Id_message in qstring and the rest of the field
___________________main.cpp______________
Code:
this,
tr("Charger les resources HW"),
"C//workspace_llb_27-05-2016/10_Terminal_Generic/configuration",
"json document (*.json)"
);
{
return;
}
in = file.readAll();
file.close();
// qWarning() << in;
m_configHw = QJsonDocument::fromJson(in.toUtf8());
buildDock();
}
// TODO
void MainWindow::buildDock()
{
dock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
QJsonObject jsonObject = m_configHw.object();
// QStringList list = jsonObject.keys();
// QVariantList list = jsonArray.toVariantList();
// QJsonArray jsonArray = jsonObject.value("common_message").toArray();
// QStringList list = jsonObject.value("common_message").toString();
QJsonArray jsonArray = jsonObject.value("common_message").toArray();
QVariantList list = jsonArray.toVariantList();
// for(int i(0); i< jsonArray.size(), i++){
// QJsonObject obj = jsonArray[i].toObject();
// list.append(obj.);
// }
// foreach(const QJsonValue & value, jsonArray){
// QJsonObject obj = value.toObject();
// list = obj.keys();
// }
foreach
(const QVariant & value, jsonArray
){
QVariantMap obj = value.toMap();
qDebug() << obj.value("Id_message");
}
// QVariantMap pair = jsonObject.toVariantMap();
// QAbstractItemModel *model = new QStringListModel(list.toStdList());
// QListView *view = new QListView;
// view->setModel(model);
// ui->dockWidgetContents->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
// dock->setWidget(view);
addDockWidget(Qt::LeftDockWidgetArea, dock);
}
Sorry It's a lot messy but my aim is to display a list of json and a least in my IHM change the values in time. how could I do a list from the field in my json ?
If you don't get it what I'm trying to do ask me question !
Thanks for any advice
Re: Using QJson for read and display A list into QStandardItemModel
You are trying to convert the value of "common_message" into an array, but it is not an array.
"common_message" is an object.
Also, don't convert the return value of QFile::readAll() needlessly into a QString and then back into a QByteArray.
Cheers,
_
Re: Using QJson for read and display A list into QStandardItemModel
oh thanks a lot indeed. I just did what you said :
[CODE]
QJsonObject JsonObject = m_configHw.Object();
QStringList list;
foreach(const QJsonValue & value, jsonObject){
QJsonObject obj = value.toObject();
list.append(obj.keys());
}
QAbstractItemModel *model = new QStringListModel(list);
QListView *view = new QListView;
view->setModel(model);
dock->setWidget(view);
[\CODE]
yeah I heard about the readall(). I'm going to change it
Cheers