QT read json keyvalue pairs in a array
up vote
0
down vote
favorite
Following JSON should be read in a qt, and stored in an array as key value pairs, so I can work with these values:
json:
{
"datasources":[
{"id":1,"name":"TPS Position"},
{"id":2,"name":"TPS Timer"},
{"id":3,"name":"Lateral G"},
{"id":4,"name":"Longitudal G"},
{"id":5,"name":"Z"},
{"id":6,"name":"AFR"}
]}
I read the QT JSON savegame example, but I can't figure out how to read Key values.
This is the qt code that I have, but I don't know how to go on:
jsonFileValue = datasourcesjson.readAll();
datasourcesjson.close();
QJsonDocument m_DataSourceDocument = QJsonDocument::fromJson(jsonFileValue.toUtf8());
QJsonObject m_DataSourceObject = m_DataSourceDocument.object();
QJsonArray m_DataSourceArray = m_DataSourceObject.value(QString("datasources")).toArray();
qWarning() << m_DataSourceArray;
This is the output:
QJsonArray([{"id":1,"name":"TPS Position"},{"id":2,"name":"TPS Timer"},{"id":3,"name":"Lateral G"},{"id":4,"name":"Longitudal G"},{"id":5,"name":"Z"},{"id":6,"name":"AFR"}])
But how can I get this data in an array to work with in my programm like QMap for key values?
json qt
add a comment |
up vote
0
down vote
favorite
Following JSON should be read in a qt, and stored in an array as key value pairs, so I can work with these values:
json:
{
"datasources":[
{"id":1,"name":"TPS Position"},
{"id":2,"name":"TPS Timer"},
{"id":3,"name":"Lateral G"},
{"id":4,"name":"Longitudal G"},
{"id":5,"name":"Z"},
{"id":6,"name":"AFR"}
]}
I read the QT JSON savegame example, but I can't figure out how to read Key values.
This is the qt code that I have, but I don't know how to go on:
jsonFileValue = datasourcesjson.readAll();
datasourcesjson.close();
QJsonDocument m_DataSourceDocument = QJsonDocument::fromJson(jsonFileValue.toUtf8());
QJsonObject m_DataSourceObject = m_DataSourceDocument.object();
QJsonArray m_DataSourceArray = m_DataSourceObject.value(QString("datasources")).toArray();
qWarning() << m_DataSourceArray;
This is the output:
QJsonArray([{"id":1,"name":"TPS Position"},{"id":2,"name":"TPS Timer"},{"id":3,"name":"Lateral G"},{"id":4,"name":"Longitudal G"},{"id":5,"name":"Z"},{"id":6,"name":"AFR"}])
But how can I get this data in an array to work with in my programm like QMap for key values?
json qt
1
Just read the documentation! - it's really not that hard guys...
– Felix
Nov 17 at 11:08
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Following JSON should be read in a qt, and stored in an array as key value pairs, so I can work with these values:
json:
{
"datasources":[
{"id":1,"name":"TPS Position"},
{"id":2,"name":"TPS Timer"},
{"id":3,"name":"Lateral G"},
{"id":4,"name":"Longitudal G"},
{"id":5,"name":"Z"},
{"id":6,"name":"AFR"}
]}
I read the QT JSON savegame example, but I can't figure out how to read Key values.
This is the qt code that I have, but I don't know how to go on:
jsonFileValue = datasourcesjson.readAll();
datasourcesjson.close();
QJsonDocument m_DataSourceDocument = QJsonDocument::fromJson(jsonFileValue.toUtf8());
QJsonObject m_DataSourceObject = m_DataSourceDocument.object();
QJsonArray m_DataSourceArray = m_DataSourceObject.value(QString("datasources")).toArray();
qWarning() << m_DataSourceArray;
This is the output:
QJsonArray([{"id":1,"name":"TPS Position"},{"id":2,"name":"TPS Timer"},{"id":3,"name":"Lateral G"},{"id":4,"name":"Longitudal G"},{"id":5,"name":"Z"},{"id":6,"name":"AFR"}])
But how can I get this data in an array to work with in my programm like QMap for key values?
json qt
Following JSON should be read in a qt, and stored in an array as key value pairs, so I can work with these values:
json:
{
"datasources":[
{"id":1,"name":"TPS Position"},
{"id":2,"name":"TPS Timer"},
{"id":3,"name":"Lateral G"},
{"id":4,"name":"Longitudal G"},
{"id":5,"name":"Z"},
{"id":6,"name":"AFR"}
]}
I read the QT JSON savegame example, but I can't figure out how to read Key values.
This is the qt code that I have, but I don't know how to go on:
jsonFileValue = datasourcesjson.readAll();
datasourcesjson.close();
QJsonDocument m_DataSourceDocument = QJsonDocument::fromJson(jsonFileValue.toUtf8());
QJsonObject m_DataSourceObject = m_DataSourceDocument.object();
QJsonArray m_DataSourceArray = m_DataSourceObject.value(QString("datasources")).toArray();
qWarning() << m_DataSourceArray;
This is the output:
QJsonArray([{"id":1,"name":"TPS Position"},{"id":2,"name":"TPS Timer"},{"id":3,"name":"Lateral G"},{"id":4,"name":"Longitudal G"},{"id":5,"name":"Z"},{"id":6,"name":"AFR"}])
But how can I get this data in an array to work with in my programm like QMap for key values?
json qt
json qt
edited Nov 17 at 11:44
asked Nov 17 at 10:52
Basti An
6514
6514
1
Just read the documentation! - it's really not that hard guys...
– Felix
Nov 17 at 11:08
add a comment |
1
Just read the documentation! - it's really not that hard guys...
– Felix
Nov 17 at 11:08
1
1
Just read the documentation! - it's really not that hard guys...
– Felix
Nov 17 at 11:08
Just read the documentation! - it's really not that hard guys...
– Felix
Nov 17 at 11:08
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Basically, you can loop through the QJsonArray and get QJsonObject from each member. Then you can get the value by its key. QJsonObject::value() returns QJsonValue type object which can be casted in your case to QString or int.
QJsonDocument m_DataSourceDocument = QJsonDocument::fromJson(jsonFileValue.toUtf8());
QJsonObject m_DataSourceObject = m_DataSourceDocument.object();
QJsonArray m_DataSourceArray = m_DataSourceObject.value(QString("datasources")).toArray();
qWarning() << m_DataSourceArray;
for (const auto m_DataSource : m_DataSourceArray)
// or instead of c++11 for-loop, you can use foreach, like:
// foreach (const QJsonValue& m_DataSource, m_DataSourceArray)
{
QJsonObject m_DataSourceObject = m_DataSource.toObject();
qWarning() << "Id: "
<< m_DataSourceObject.value("id").toInt()
<< " and name: "
<< m_DataSourceObject.value("name").toString();
}
Outputs:
Id: 1 and name: "TPS Position"
Id: 2 and name: "TPS Timer"
Id: 3 and name: "Lateral G"
Id: 4 and name: "Longitudal G"
Id: 5 and name: "Z"
Id: 6 and name: "AFR"
If you don't want to loop through all members, you can also read a single value from array.
Following code prints 'name' value of the second member:
qWarning() << m_DataSourceArray.at(1).toObject().value("name").toString();
Outputs:
"TPS Timer"
Hope this helps.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Basically, you can loop through the QJsonArray and get QJsonObject from each member. Then you can get the value by its key. QJsonObject::value() returns QJsonValue type object which can be casted in your case to QString or int.
QJsonDocument m_DataSourceDocument = QJsonDocument::fromJson(jsonFileValue.toUtf8());
QJsonObject m_DataSourceObject = m_DataSourceDocument.object();
QJsonArray m_DataSourceArray = m_DataSourceObject.value(QString("datasources")).toArray();
qWarning() << m_DataSourceArray;
for (const auto m_DataSource : m_DataSourceArray)
// or instead of c++11 for-loop, you can use foreach, like:
// foreach (const QJsonValue& m_DataSource, m_DataSourceArray)
{
QJsonObject m_DataSourceObject = m_DataSource.toObject();
qWarning() << "Id: "
<< m_DataSourceObject.value("id").toInt()
<< " and name: "
<< m_DataSourceObject.value("name").toString();
}
Outputs:
Id: 1 and name: "TPS Position"
Id: 2 and name: "TPS Timer"
Id: 3 and name: "Lateral G"
Id: 4 and name: "Longitudal G"
Id: 5 and name: "Z"
Id: 6 and name: "AFR"
If you don't want to loop through all members, you can also read a single value from array.
Following code prints 'name' value of the second member:
qWarning() << m_DataSourceArray.at(1).toObject().value("name").toString();
Outputs:
"TPS Timer"
Hope this helps.
add a comment |
up vote
0
down vote
Basically, you can loop through the QJsonArray and get QJsonObject from each member. Then you can get the value by its key. QJsonObject::value() returns QJsonValue type object which can be casted in your case to QString or int.
QJsonDocument m_DataSourceDocument = QJsonDocument::fromJson(jsonFileValue.toUtf8());
QJsonObject m_DataSourceObject = m_DataSourceDocument.object();
QJsonArray m_DataSourceArray = m_DataSourceObject.value(QString("datasources")).toArray();
qWarning() << m_DataSourceArray;
for (const auto m_DataSource : m_DataSourceArray)
// or instead of c++11 for-loop, you can use foreach, like:
// foreach (const QJsonValue& m_DataSource, m_DataSourceArray)
{
QJsonObject m_DataSourceObject = m_DataSource.toObject();
qWarning() << "Id: "
<< m_DataSourceObject.value("id").toInt()
<< " and name: "
<< m_DataSourceObject.value("name").toString();
}
Outputs:
Id: 1 and name: "TPS Position"
Id: 2 and name: "TPS Timer"
Id: 3 and name: "Lateral G"
Id: 4 and name: "Longitudal G"
Id: 5 and name: "Z"
Id: 6 and name: "AFR"
If you don't want to loop through all members, you can also read a single value from array.
Following code prints 'name' value of the second member:
qWarning() << m_DataSourceArray.at(1).toObject().value("name").toString();
Outputs:
"TPS Timer"
Hope this helps.
add a comment |
up vote
0
down vote
up vote
0
down vote
Basically, you can loop through the QJsonArray and get QJsonObject from each member. Then you can get the value by its key. QJsonObject::value() returns QJsonValue type object which can be casted in your case to QString or int.
QJsonDocument m_DataSourceDocument = QJsonDocument::fromJson(jsonFileValue.toUtf8());
QJsonObject m_DataSourceObject = m_DataSourceDocument.object();
QJsonArray m_DataSourceArray = m_DataSourceObject.value(QString("datasources")).toArray();
qWarning() << m_DataSourceArray;
for (const auto m_DataSource : m_DataSourceArray)
// or instead of c++11 for-loop, you can use foreach, like:
// foreach (const QJsonValue& m_DataSource, m_DataSourceArray)
{
QJsonObject m_DataSourceObject = m_DataSource.toObject();
qWarning() << "Id: "
<< m_DataSourceObject.value("id").toInt()
<< " and name: "
<< m_DataSourceObject.value("name").toString();
}
Outputs:
Id: 1 and name: "TPS Position"
Id: 2 and name: "TPS Timer"
Id: 3 and name: "Lateral G"
Id: 4 and name: "Longitudal G"
Id: 5 and name: "Z"
Id: 6 and name: "AFR"
If you don't want to loop through all members, you can also read a single value from array.
Following code prints 'name' value of the second member:
qWarning() << m_DataSourceArray.at(1).toObject().value("name").toString();
Outputs:
"TPS Timer"
Hope this helps.
Basically, you can loop through the QJsonArray and get QJsonObject from each member. Then you can get the value by its key. QJsonObject::value() returns QJsonValue type object which can be casted in your case to QString or int.
QJsonDocument m_DataSourceDocument = QJsonDocument::fromJson(jsonFileValue.toUtf8());
QJsonObject m_DataSourceObject = m_DataSourceDocument.object();
QJsonArray m_DataSourceArray = m_DataSourceObject.value(QString("datasources")).toArray();
qWarning() << m_DataSourceArray;
for (const auto m_DataSource : m_DataSourceArray)
// or instead of c++11 for-loop, you can use foreach, like:
// foreach (const QJsonValue& m_DataSource, m_DataSourceArray)
{
QJsonObject m_DataSourceObject = m_DataSource.toObject();
qWarning() << "Id: "
<< m_DataSourceObject.value("id").toInt()
<< " and name: "
<< m_DataSourceObject.value("name").toString();
}
Outputs:
Id: 1 and name: "TPS Position"
Id: 2 and name: "TPS Timer"
Id: 3 and name: "Lateral G"
Id: 4 and name: "Longitudal G"
Id: 5 and name: "Z"
Id: 6 and name: "AFR"
If you don't want to loop through all members, you can also read a single value from array.
Following code prints 'name' value of the second member:
qWarning() << m_DataSourceArray.at(1).toObject().value("name").toString();
Outputs:
"TPS Timer"
Hope this helps.
edited Nov 18 at 13:16
answered Nov 18 at 11:29
Rob
868
868
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53350502%2fqt-read-json-keyvalue-pairs-in-a-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Just read the documentation! - it's really not that hard guys...
– Felix
Nov 17 at 11:08