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