Читать данные из файла .txt (например, погода: blabla: sunny < /code>). < /p>
< /li>
. />
Во время преобразования .txt → .json последний сегмент (например, «солнечный») отрезан. < /p>
. Программное обеспечение, конфиденциальные данные. < /li>
< /ul>
Ссылка на приложение:
Google Drive
run: in vs код → терминал → npx electron. < /p>
Ожидаемый формат. (Погода: Blabla; Blabla: Sunny). < /p>
Вопрос о том, как сделать так, чтобы после второго ': «он читает данные в json, в то время как остальная часть файла TXT читается правильно? class = "Snippet-Code-JS Lang-JS PrettyPrint-Override">
Код: Выделить всё
// Function to convert TXT file content to JSON
function convertToJson(text) {
console.log("Input data:", text); // For debugging
const lines = text.split('\n').filter(line => line.trim());
const result = {};
for (const line of lines) {
console.log("Processed line:", line); // For debugging
// Skip empty lines
if (!line.trim()) continue;
// Find the position of the first colon
const firstColonIndex = line.indexOf(':');
// If there’s no colon, skip to the next line
if (firstColonIndex === -1) continue;
// Extract the key (before the first colon)
const key = line.substring(0, firstColonIndex).trim();
// Extract the full value after the first colon
const fullValue = line.substring(firstColonIndex + 1);
console.log(`Found key: "${key}", value: "${fullValue}"`); // For debugging
// Save to the result object
result[key] = fullValue.trim();
}
console.log("JSON result:", result); // For debugging
return result;
}
// Test
const testData = "weather:sunny:flower";
const result = convertToJson(testData);
console.log(JSON.stringify(result, null, 2));Подробнее здесь: https://stackoverflow.com/questions/794 ... javascript
Мобильная версия