就像是:
var jsonString = '{ "Id": 1, "Name": "Coke" }';
//should be true
IsJsonString(jsonString);
//should be false
IsJsonString("foo");
IsJsonString("<div>foo</div>")
Run Code Online (Sandbox Code Playgroud)
解决方案不应包含try/catch.我们中的一些人打开"中断所有错误",他们不喜欢调试器打破那些无效的JSON字符串.
我正在使用Stripe.js元素来构建应用程序。使用Stripe提供的测试卡号,我没有问题。所有工作均按预期进行。
我现在专注于错误处理。当使用测试卡4000 0000 0000 0002处理故意被拒绝的卡时,出现此错误:
致命错误:未捕获的Stripe \ Error \ Card:您的卡被拒绝。在C:\ Apache24 \ htdocs ... \ vendor \ stripe \ stripe-php \ lib \ ApiRequestor.php:128中,来自API请求{token}。...
现在,我假设这不是PHP致命错误(无法在try / catch块中处理),因此我搜索并找到了此示例并将其实现到我的代码中,如下所示:
\Stripe\Stripe::setApiKey(self::APIKEY);
$charge_arr = array(
"key" => value, etc ... )
try {
$charge = \Stripe\Charge::create($charge_arr);
if($charge->paid == true) {
echo '<br>the card was successfully charged!';
}
} catch (\Stripe\Error\Base $e) {
echo '<br>base';
echo ($e->getMessage());
} catch (\Stripe\Error\Card $e) {
$body = $e->getJsonBody();
$err = $body['error'];
print('Status is:' . $e->getHttpStatus() …
Run Code Online (Sandbox Code Playgroud)