Tengo un JSON del servidor web que se ve así:
{"timestamp":1642069251.6908009,"station_id":"ORBB","channel":5,"freq":131.725,"level":-28.1,"error":0,"mode":"2","label":"Q0","block_id":"8","ack":false,"tail":"A6-ANR","flight":"G90369","msgno":"S10A"} {"timestamp":1642069257.00876,"station_id":"ORBB","channel":5,"freq":131.725,"level":-41.3,"error":0,"mode":"2","label":"Q0","block_id":"7","ack":false,"tail":"PH-HXO","flight":"HV6905","msgno":"S28A"} {"timestamp":1642069259.057013,"station_id":"ORBB","channel":5,"freq":131.725,"level":-24.9,"error":0,"mode":"D","label":"Q0","block_id":"9","ack":false,"tail":"A6-ANR","flight":"G90369","msgno":"S11A"}He seguido esta respuesta pero no tengo nada que mostrar en la consola.
No puedo editar el JSON de datos anterior para entregar un JSON válido porque proviene de un servidor de datos que usa Ajax.
$(document).ready(() => { $.ajax('acarsdec.json', { type: 'GET', //dataType: "text", timeout: 10000, cache: false, }).done(function (data, textStatus, jqXHR) { const dataasfile = `data:text/plain;base64,${btoa(data)}`; fetch(dataasfile).then(res => res.text()).then(text => { let jsonstr = `[${text.split('\n').join(',')}]`; let json = JSON.parse(jsonstr); console.log(json); }); }) })Como punto de partida, el fragmento de código que muestra no es JSON válido, por lo que no hay motivo para intentar analizarlo como JSON.
Si puede confiar en que la fuente es "pseudo" JSON con un objeto por línea, puede dividir el texto en una nueva línea (\n) y unir la cadena nuevamente con una coma (,) y colocar corchetes alrededor de la cadena:
let jsonstr = `[${text.split('\n').join(',')}]`;Aquí uso la función de búsqueda y una URL de datos para imitar la solicitud de AJAX.
/***** start setup for demostration *****/ const data = `{"timestamp":1642069251.6908009,"station_id":"ORBB","channel":5,"freq":131.725,"level":-28.1,"error":0,"mode":"2","label":"Q0","block_id":"8","ack":false,"tail":"A6-ANR","flight":"G90369","msgno":"S10A"} {"timestamp":1642069257.00876,"station_id":"ORBB","channel":5,"freq":131.725,"level":-41.3,"error":0,"mode":"2","label":"Q0","block_id":"7","ack":false,"tail":"PH-HXO","flight":"HV6905","msgno":"S28A"} {"timestamp":1642069259.057013,"station_id":"ORBB","channel":5,"freq":131.725,"level":-24.9,"error":0,"mode":"D","label":"Q0","block_id":"9","ack":false,"tail":"A6-ANR","flight":"G90369","msgno":"S11A"}`; const dataasfile = `data:text/plain;base64,${btoa(data)}`; /***** end setup for demostration *****/ /* replace dataasfile with your URL to the API */ fetch(dataasfile).then(res => res.text()).then(text => { let jsonstr = `[${text.split('\n').join(',')}]`; let json = JSON.parse(jsonstr); console.log(json); });OP todavía tiene problemas para analizar la cadena de la solicitud. Aparentemente, también hay una línea en blanco al final de la cadena. En el siguiente ejemplo, agregué una línea en blanco al final de la cadena. Luego filtro la matriz, solo acepto elementos en la matriz que no son una cadena vacía. Esta línea:
let jsonstr = `[${text.split('\n').filter(str => str != '').join(',')}]`; /***** start setup for demostration *****/ const data = `{"timestamp":1642069251.6908009,"station_id":"ORBB","channel":5,"freq":131.725,"level":-28.1,"error":0,"mode":"2","label":"Q0","block_id":"8","ack":false,"tail":"A6-ANR","flight":"G90369","msgno":"S10A"} {"timestamp":1642069257.00876,"station_id":"ORBB","channel":5,"freq":131.725,"level":-41.3,"error":0,"mode":"2","label":"Q0","block_id":"7","ack":false,"tail":"PH-HXO","flight":"HV6905","msgno":"S28A"} {"timestamp":1642069259.057013,"station_id":"ORBB","channel":5,"freq":131.725,"level":-24.9,"error":0,"mode":"D","label":"Q0","block_id":"9","ack":false,"tail":"A6-ANR","flight":"G90369","msgno":"S11A"} `; // blank line here in the end on purpose! const dataasfile = `data:text/plain;base64,${btoa(data)}`; /***** end setup for demostration *****/ /* replace dataasfile with your URL to the API */ fetch(dataasfile).then(res => res.text()).then(text => { let jsonstr = `[${text.split('\n').filter(str => str != '').join(',')}]`; let json = JSON.parse(jsonstr); console.log(json); });