解析 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,拋出IfError() )
故障安全 JSON 解析