我是Javascript的新手,試圖創建一個使用JSON.parse()函式的專案。 我的代碼運行成功,但是JSON.parse沒有作業,而且在開發工具的控制臺日志中沒有回傳任何錯誤。
我試著用以下方法來除錯我的代碼:
console.log("Test log1#######"/span>)
console.log(JSON.parse(json))
console.log("Test log2#######"/span>)
在console.log上,我只能看到"Test log1#######"。但沒有看到"Test log2#######",這意味著代碼在第二行失敗,但沒有在Devtools控制臺列印任何錯誤,因為它通常是這樣。但我確信代碼的失敗是由于一些JSON驗證問題,但我在開發工具上看不到這個錯誤。
請建議一種方法來探索JSON.parse()的錯誤日志
。謝謝
uj5u.com熱心網友回復:
你可以這樣做:
try{
JSON.parse(badFormat)。
}catch(err){
console.log(err)。
}
uj5u.com熱心網友回復:
很可能你的 "json "字串是未定義的
。嘗試使用reviver引數來查看json字串的內容。JSON.parse()如果發現傳遞的字串有問題,就會在瀏覽器的控制臺標簽中吐出錯誤
。span class="hljs-variable constant_">JSON。 parse('your string', (key, value) => {
console.log(key); //記錄當前屬性名稱,最后是""。
return value; // return the unchanged property value.
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/307059.html
標籤:
