json_encode()期望参数2长,给定字符串 [英] json_encode() expects parameter 2 to be long, string given

查看:713
本文介绍了json_encode()期望参数2长,给定字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码从REST服务返回JSON:

I'm trying to return a JSON from a REST Service using this code:

$categories = $categoriesController->listAll();
if($categories){
   header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
   header("Content-Type: application/json");
   echo json_encode($categories,JSON_PRETTY_PRINT);
}else{

}

但是我得到这个错误:

json_encode()期望参数2长,并给出字符串

json_encode() expects parameter 2 to be long, string given

我已经看到几个示例使用完全相同的代码,所以我不明白为什么我遇到了这个问题.我希望在此问题上有所帮助.谢谢:)

I've seen several examples use the exact same code so I don't understand why i'm getting this issue. I'd appreciate some help with this problem. Thanks :)

推荐答案

常量JSON_PRETTY_PRINT仅适用于PHP版本≥5.4. 它的值为128,因此请尝试将JSON_PRETTY_PRINT替换为128

The constant JSON_PRETTY_PRINT is only available for PHP versions >= 5.4. It's value is 128, so try replacing JSON_PRETTY_PRINT with 128

echo json_encode($categories,128);

这篇关于json_encode()期望参数2长,给定字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆