- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
当通过 json_decode() 解析时,我正在尝试处理错误的 json 数据。我正在使用以下脚本:
if(!json_decode($_POST)) {
echo "bad json data!";
exit;
}
如果 $_POST 等于:
'{ bar: "baz" }'
然后 json_decode 处理错误并吐出“bad json data!”;但是,如果我将 $_POST 设置为“无效数据”之类的东西,它会给我:
Warning: json_decode() expects parameter 1 to be string, array given in C:\server\www\myserver.dev\public_html\rivrUI\public_home\index.php on line 6
bad json data!
我是否需要编写一个自定义脚本来检测有效的 json 数据,还是有其他一些不错的方法来检测这个?
最佳答案
这里有几件事是关于 json_decode
:
null
null
:当JSON字符串包含null
要解决警告问题,一个解决方案是使用 @
operator (我不经常推荐使用它,因为它会使调试变得更加困难......但是在这里,没有太多选择):
$_POST = array(
'bad data'
);
$data = @json_decode($_POST);
然后您必须测试 $data
是否为 null
-- 并且,为了避免 json_decode
返回 的情况>null
表示 JSON 字符串中的 null
,您可以查看 json_last_error
,其中(引用):
Returns the last error (if any) occurred by last JSON parsing.
这意味着您必须使用如下代码:
if ($data === null
&& json_last_error() !== JSON_ERROR_NONE) {
echo "incorrect data";
}
关于php - 在 PHP json_decode() 中检测错误的 json 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2348152/
json_decode($var) 和 (object)json_decode($var, true) 有区别吗? 当最近在 Joomla virutemart 中处理某段代码时,我遇到了一个困惑的情
我正在尝试使用 PHP 和 json_decode 解析 JSON 文件,但是当返回的 JSON 是命名空间时,我很难做到这一点。例如: $json_ouput = json_decode($json
我返回了这个 RAW JSON,当我应用 json_decode 以数组形式访问数据时,它会将 trackingNumber 转换为科学记数法。有没有什么可能的方法可以避免这种情况而不会影响/舍入整数
当我将带有前导零数字的电话号码(即字符串)传递给 json_decode 时, 它删除第一个零字符并将其转换为浮点数。 $string = '09178882356'; //dynamic value
由于某种原因,我无法从返回的 jsonp 字符串中获取信息, add ?vorto=TheWordYouWant to the end of this website"; echo $Help;
我只是从 php 文档测试这个示例 ( http://au2.php.net/manual/en/function.json-decode.php ) 这是我的代码: ';?> 但它只返回一个空数组。
我有一个包含多个对象的 JSON 数组,并尝试使用 json_decode 来创建关联数组。 示例数据 $json='[{ type: "cool", cate
我有类似以下代码: 结果如下: int(9223372036854775807) 当然,这不是 json 字符串,我希望 json_decode 返回 null 或 false,如 manual 中
我有一个以下 JSON 字符串,通过 AJAX 到达服务器: {"Names":"[{0:'asdasd'}]","Values":"[{0:'ad'}]"} 如您所见,名称和值旨在保存一个数组。问题
通过示例最容易解释: var_dump(json_decode("[{'a':'b'},{'c':'d'},{'e':'f'}]")); // NULL var_dump(json_decode('[
我正在使用来自 deepl.com 的 API,它提供自动翻译。我通过 cURL 调用 API,并得到一个 json 字符串作为返回,该字符串看起来没问题,但由于某种原因无法被 PHP 解码。 让我首
我有这样编码的 JSON 对象 { "id": "", "steps": [ { "target": "purchase_order_items_itemma
这个问题在这里已经有了答案: Get information from JSON (2 个答案) 关闭 11 个月前。 我一直在尝试使用来自各种服务的一些 API,目前我正在尝试检索有关 vimeo
我有一个包含多个对象的 JSON 数组,并尝试使用 json_decode 来创建一个关联数组。 示例数据 $json='[{ type: "cool", ca
我有类似以下代码: 结果如下: int(9223372036854775807) 当然,这不是 json 字符串,我希望 json_decode 返回 null 或 false,如 manual 中
通过示例最容易解释: var_dump(json_decode("[{'a':'b'},{'c':'d'},{'e':'f'}]")); // NULL var_dump(json_decode('[
我有一个像这样的二维对象数组: function test(n){ this.id = n; } var testArray= new Array(2); for(i = 0; i 但是我得到一
我正在以这种方式在 javascript 中创建一个 json jsonArr.push({ position: 'WN', wind: windWN,
$objetos = json_decode($_POST['objetos']); $query1 = "DELETE FROM `usuarioObjeto` WHERE idusuario="
我刚刚开始真正使用 json,并尝试尽我所能地学习!我想分享我所做的这项工作,我觉得这可能需要一些改进,如果不是大部分的话。 好吧,我使用 twitch.tv REST_API。这是我的代码。基本上我
我是一名优秀的程序员,十分优秀!