Javascript Отображение ошибки для некорректного JSON объекта в цикле - PullRequest
0 голосов
/ 22 января 2020

Я пишу сценарий, который загружает массив из JSON объектов из файла и проверяет, не поврежден ли отдельный элемент или Json объект, а затем выдает ошибку. я хочу запустить al oop в массиве, чтобы вывести список неисправных json объектов. пожалуйста, как я go об этом.

вот мой код.

           //function to load the json file from the host  - works perfectly.. 

             function loadJsonFile(file, callback) {
                var rawFile = new XMLHttpRequest();
                rawFile.overrideMimeType("application/json");
                rawFile.open("GET", file, true);
                rawFile.onreadystatechange = function() {
                    if (rawFile.readyState === 4 && rawFile.status == "200") {
                        callback(rawFile.responseText);
                    }
                }
                rawFile.send(null);
            }

            //load the json file and parse the data. 

            loadJsonFile("test_data.json", function(text){
                var data = JSON.parse(text);               
                runTestCases(data);  
            });


            //run a test on each json object and log out the faulty ones.. 
            //if the loop throws an error at any point it should continue to the nest node
            //within the loop,and should not stop there until it has finished.

            const runTestCases = (obj) => {

                obj.forEach((o, v)=>{

                    try{

                       // how do test for a malformed json object

                    }catch(error){

                       throw new Error('this json object wasnt formed properly');

                     }
                    });

                console.log(testShape);
            }
...