Reach the spesific element of Json Array
-
@suslucoder said in Reach the spesific element of Json Array:
qDebug() << inner_data[0].toString(); // "integer"
qDebug() << inner_data[1].toString(); // "0" -
@suslucoder
QJsonValue
is something which behaves like aQVariant
. It can hold any of the (6) types returned byQJsonValue::Type QJsonValue::type()
.You can use any of the
QJsonValue::is...()
methods to test for a particular type.If you know what your type is in advance, or you test it as above to make sure, for your case you can use:
QJsonValue("integer").toString(); // `"integer"`, as a string QJsonValue("0").toInt(); // `0`, as an integer
-
@JonB well, thank you so much
What if i want to short this code... Im trying to give or operand like
inner_data[1].toString().toInt() // it print the 0 for 3 times and the others as well
I know it isnt healthy for writing much more if statements but i foul up
if (cit.key() == "GPS ID" || cit.key() == "GPS Mod" || cit.key() == "GPS Uydu Sayısı") { QJsonArray inner_data = cit.value().toArray(); gps[inner_data[1].toString().toInt()].i; // "0" }
``` if (inner_data[0] == "integer") { if (cit.key() == "GPS ID) { QJsonArray inner_data = cit.value().toArray(); gps[inner_data[1].toString().toInt()].i; // "0" } if (cit.key() == "GPS Mod") { QJsonArray inner_data = cit.value().toArray(); gps[inner_data[1].toString().toInt()]; } if (cit.key() == "GPS Uydu Sayısı") { QJsonArray inner_data = cit.value().toArray(); gps[inner_data[1].toString().toInt()].i; // "0" } }
-
@suslucoder
I don't really know what your latest question is.Forinner_data[1].toString().toInt()
it is pointless to convert to string and then to integer, as I wrote earlier you might as well goinner_data[1].toInt()
.Yes you can do all 3 via
if (cit.key() == "GPS ID" || cit.key() == "GPS Mod" || cit.key() == "GPS Uydu Sayısı") { QJsonArray inner_data = cit.value().toArray(); int intValue = inner_data[1].toString().toInt(); ... }
-
@JonB said in Reach the spesific element of Json Array:
wrote earlier you might as well go inner_data[1].toInt()
No because the json contains a string, not an integer. The json is 'wrong' in this way - when an integer should be stored/read then it should be stored as integer and not as string.
-
@Christian-Ehrlicher
You are right. I had not fully noticed the"0"
string-type (and similarly with the other numbers).@suslucoder
Whatever produces our JSON (you? something else?) would be better outputting the numbers as numbers (no quotes) rather than as strings.If the JSON had
"GPS ID":[ "integer", 0 ],
then you could go
inner_data[1].toInt()
. However if it is"GPS ID":[ "integer", "0" ],
then you must indeed go
inner_data[1].toString().toInt()
. I have corrected my earlier accordingly. -
@suslucoder said in Reach the spesific element of Json Array:
I expect that it should give me
That is what it will do.
But it prints
Sounds like you are printing the same value 3 times each time. No idea why you would do that, or what you would expect. If you have 3 prints (or 9 prints), that is what you will get....