Ich habe ein GUI-Programm zum Lesen von XML-Dateien, ich habe zwei Funktionen zum Konvertieren von XML in JSON geschrieben, aber aus irgendeinem Grund geht der Wert des letzten Elements während der Konvertierung verloren. Das angehängte Bild zeigt mein ursprüngliches XML und das daraus abgeleitete JSON.
Wie kann das behoben werden?
Hier sind meine Methoden:
QJsonObject QtXML::modelIndexToJson(const QModelIndex &index,
QtTreeModel *model) {
QJsonObject jsonObject;
// Get the node name
QString key = model->data(index, Qt::DisplayRole).toString();
// Check if there is a text value
QString value = model->data(index, Qt::EditRole).toString();
// If there is a text value, save it
if (!value.isEmpty()) {
jsonObject[key] = value;
} else {
// If there is no text value, process child elements
QJsonObject childrenObject;
for (int i = 0; i < model->rowCount(index); ++i) {
QModelIndex childIndex = model->index(i, 0, index);
childrenObject.insert(model->data(childIndex, Qt::DisplayRole).toString(),
modelIndexToJson(childIndex, model));
}
jsonObject[key] = childrenObject;
}
return jsonObject;
}
// Method to convert data from QTreeView to JSON and save it to a file
void QtXML::convertXmlToJson(const QString &jsonFilePath) {
// Get the current view (QTreeView) from the tab
auto *currView = qobject_cast(tabWgt->currentWidget());
if (!currView) {
qWarning("The current view is not a QTreeView.");
return;
}
// Get the model of the current view
auto *currModel = dynamic_cast(currView->model());
if (!currModel) {
qWarning("The current view's model is not a QtTreeModel.");
return;
}
// Convert the root elements of the model into JSON
QJsonObject rootObject;
for (int i = 0; i < currModel->rowCount(); ++i) {
QModelIndex rootIndex = currModel->index(i, 0);
rootObject.insert(currModel->data(rootIndex, Qt::DisplayRole).toString(),
modelIndexToJson(rootIndex, currModel));
}
// Create JSON document
QJsonDocument jsonDoc(rootObject);
// Write JSON to file
QFile jsonFile(jsonFilePath);
if (!jsonFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
qWarning("Failed to open JSON file for writing");
return;
}
jsonFile.write(jsonDoc.toJson(QJsonDocument::Indented));
jsonFile.close();
qInfo("Data from QTreeView successfully converted to JSON and written to file");
}
Attraction registered
1058514543
77-0234
68350f83-f3d1-49e1-bdfc-739647cea2df
4
55.7875
37.6560
Park
Sokolniki District
Happy Swing
Eastern Administrative Okrug
SOKOLNIKI CULTURE AND RECREATION PARK
Ich habe ein GUI-Programm zum Lesen von XML-Dateien, ich habe zwei Funktionen zum Konvertieren von XML in JSON geschrieben, aber aus irgendeinem Grund geht der Wert des letzten Elements während der Konvertierung verloren. Das angehängte Bild zeigt mein ursprüngliches XML und das daraus abgeleitete JSON. Wie kann das behoben werden? Hier sind meine Methoden: [code]QJsonObject QtXML::modelIndexToJson(const QModelIndex &index, QtTreeModel *model) { QJsonObject jsonObject;
// Get the node name QString key = model->data(index, Qt::DisplayRole).toString();
// Check if there is a text value QString value = model->data(index, Qt::EditRole).toString();
// If there is a text value, save it if (!value.isEmpty()) { jsonObject[key] = value; } else { // If there is no text value, process child elements QJsonObject childrenObject; for (int i = 0; i < model->rowCount(index); ++i) { QModelIndex childIndex = model->index(i, 0, index); childrenObject.insert(model->data(childIndex, Qt::DisplayRole).toString(), modelIndexToJson(childIndex, model)); } jsonObject[key] = childrenObject; }
return jsonObject; }
// Method to convert data from QTreeView to JSON and save it to a file void QtXML::convertXmlToJson(const QString &jsonFilePath) { // Get the current view (QTreeView) from the tab auto *currView = qobject_cast(tabWgt->currentWidget()); if (!currView) { qWarning("The current view is not a QTreeView."); return; }
// Get the model of the current view auto *currModel = dynamic_cast(currView->model()); if (!currModel) { qWarning("The current view's model is not a QtTreeModel."); return; }
// Convert the root elements of the model into JSON QJsonObject rootObject; for (int i = 0; i < currModel->rowCount(); ++i) { QModelIndex rootIndex = currModel->index(i, 0); rootObject.insert(currModel->data(rootIndex, Qt::DisplayRole).toString(), modelIndexToJson(rootIndex, currModel)); }
// Write JSON to file QFile jsonFile(jsonFilePath); if (!jsonFile.open(QIODevice::WriteOnly | QIODevice::Text)) { qWarning("Failed to open JSON file for writing"); return; }
jsonFile.write(jsonDoc.toJson(QJsonDocument::Indented)); jsonFile.close(); qInfo("Data from QTreeView successfully converted to JSON and written to file"); } [/code] Beispielarbeit von Methoden. Quell-XML-Datei: [code]
Ich habe ein Problem mit der Bibliothek jackson-dataformat-xml, wenn ich nach der Migration meines Projekts JSON in XML umwandele. Die generierte XML-Ausgabe hat sich unerwartet geändert, was zu...
Ich bin ziemlich neu in HTML und Softwareentwicklung
Ich plane, Inhalte aus HTML im JSON -Format zu extrahieren, so dass nur bestimmte Abschnitte von HTML extrahiert werden sollten. Und ich kann es...
Ich erhalte diese Fehlermeldung:
Diese Version versteht nur SDK-XML-Versionen bis 2, es wurde jedoch eine SDK-XML-Datei der Version 3 gefunden. Dies kann passieren, wenn Sie Versionen von Android...