Parse JSON () 很難使用,因為它不能很好地處理錯誤。在回調中(可能來自 RunProgram 或按鈕),它將建立一個不需要的模型對話方塊來宣布失敗。它還會在日誌中宣布失敗。需要額外的 JSL 來修復它:
New Window( "parsejson",
Button Box( "parse",
local( {birestore, errText},
birestore = Batch Interactive( 1 ); // makes an alert dialog from a button without this
errText = Log Capture( p = Parse JSON( "\!n" /* invalid json error */ ) ); // writes to the log without this
Batch Interactive( birestore ); // pop the previous behavior
If( Length( errText ), Throw( "Caught an error:" || char(errText) ) ); // handle unexpected error, somehow
/* use p here */
)
)
);
建議:新增一個可選參數來更改行為,例如 parseJson(x, throwIfError() )
故障安全 JSON 解析