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 解析