I'm trying to write data to JSON file with this code.

 { QJsonObject dataIndex; QString filename = QFileDialog::getSaveFileName(this, tr("Flugweg spreichern"), "", tr("Wegpunkt Datei (*.wpf)")); QFile file(filename); for(int i = 0; i < MapView->waypointData.size(); i++){ dataIndex.insert(QString("waypoint00%1").arg(MapView->waypointData[i].iD), QString("%1, %2, %3").arg(MapView->waypointData[i].lng, 0, 'f', 5).arg(MapView->waypointData[i].lat, 0, 'f', 5).arg(MapView->waypointData[i].alt, 0, 'f', 5)); } file.open(QIODevice::WriteOnly); QJsonDocument json_doc(dataIndex); QString json_string = json_doc.toJson(); QFile save_file(filename); if(!save_file.open(QIODevice::WriteOnly)){ qDebug() << "failed to open save file"; } save_file.write(json_string.toLocal8Bit()); save_file.close(); } 

I get this conclusion:

 { "waypoint000": "10.14200, 52.99339, 0.00000", "waypoint001": "10.15273, 52.99664, 0.00000", "waypoint0010": "10.17547, 52.98598, 0.00000", "waypoint0011": "10.17605, 52.98421, 0.00000", "waypoint0012": "10.17597, 52.98104, 0.00000", "waypoint0013": "10.17561, 52.97992, 0.00000", "waypoint0014": "10.17389, 52.97810, 0.00000", "waypoint0015": "10.17194, 52.97723, 0.00000", "waypoint0016": "10.16719, 52.97632, 0.00000", "waypoint0017": "10.15920, 52.97585, 0.00000", "waypoint0018": "10.15561, 52.97572, 0.00000", "waypoint002": "10.15971, 52.99664, 0.00000", "waypoint003": "10.16554, 52.99577, 0.00000", "waypoint004": "10.16928, 52.99421, 0.00000", "waypoint005": "10.17058, 52.99326, 0.00000", "waypoint006": "10.17130, 52.99235, 0.00000", "waypoint007": "10.17288, 52.99053, 0.00000", "waypoint008": "10.17389, 52.98949, 0.00000", "waypoint009": "10.17453, 52.98819, 0.00000", "waypoint0099": "10.14107, 52.97970, 0.00000" } 

How to force JSON save 0010, after 009? I read that there is no such sorting in JSON.

  • The order of the objects is indeed not guaranteed. He is not needed, because they are addressed by keys. Arrangements exist for ordering. - Kirill Malyshev
  • @ Kirill Malyshev, even if the key is being accessed, the outcome is still not right for me. In order not to fix a lot of functions. I want to make the order of objects - Insider
  • If you work with JSON as with JSON, and not as with a string, there can be no problems. Maybe you would be more comfortable with this structure pastebin.com/WGJWNKkW ? Just how to do it in C ++ I do not know. - Kirill Malyshev
  • Open Wikipedia: An object is an unordered set of key: value pairs . All the answer to your question - no way. If you want to keep order, use an array. Or generate json manually. - yrHeTaTeJlb
  • @ Kirill Malyshev did something similar to python too .. but something doesn't want to be in C ++ - Insider

1 answer 1

CSV Record:

 QFile file(fileName); QTextStream stream(&file); if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { //TODO кинуть исключение, или выдать окно ошибки или ругнуться в лог return; } for(int i = 0; i < MapView->waypointData.size(); i++) { stream << QString("waypoint00%1, %2, %3, %4\n").arg(MapView->waypointData[i].iD).arg(MapView->waypointData[i].lng, 0, 'f', 5).arg(MapView->waypointData[i].lat, 0, 'f', 5).arg(MapView->waypointData[i].alt, 0, 'f', 5); } 

CSV reading:

 QFile file(fileName); QTextStream stream(&file); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { //TODO кинуть исключение, или выдать окно ошибки или ругнуться в лог return; } QString buf; QStringList list; while (!stream.atEnd()) { buf = stream.readLine(); list = buf.split(","); //разделение делается по запятым, убедитесь, что разделитель целой и дробной части в координатах точка, не запятая if (list.size() < 4) //если вдруг попалась пустая или битая строка continue; WaypointData* w = new WaypointData(); //TODO проверить корректность конвертации с помощью bool параметра, передаваемого в w->alt = list[1].trimmed().toDouble(); //возможно, trimmed можно убрать, эта функция убирает пробелы по краям строки w->lng = list[2].trimmed().toDouble(); w->lat = list[3].trimmed().toDouble(); w->state = 0; QString id = list[0]; w->iD = id.remove(0,8).toInt(); MapView->AddWaypoint(*w); } 

The waypoint entry code is copied from your code, check that everything is being done correctly.

  • Thank you very much, one key question is my QString line = obj[key].toString(); Ie figure (0-99) here it turns out this list[0].toDouble(); ? - Insider
  • Yes, that's right, the code is not correct. Only not toDouble() but simply list[0] , the waypoint name is a string. - Bearded Beaver