我在一个代码点火器REST应用程序中有以下代码(使用:https://github.com/chriskacerguis/codeigniter-restserver构建)
public function fullname_get()
{
$fullname = array("fname"=>"john", "lname"=>"doe");
$data["json"] = json_encode($fullname);
$this->response($data["json"], 200);
}当我调用API时,返回的json如下所示:
{\"fname\":\"john\",\"lname\":\"doe\"}由于转义字符"\“,上述json字符串无法通过http://jsonlint.com/测试。只是想知道我怎么处理这件事?我正在构建一个REST应该返回json ..。我得确保这是合法的。
谢谢。
发布于 2015-01-15 22:31:03
尝尝这个
$fullname = array("fname"=>"john", "lname"=>"doe");
$this->response($fullname, 200);//it sends data json format. You don't need to json encode it您得到了这个响应,因为您的数据被json编码了两次。
发布于 2015-01-15 20:52:43
它是合法的JSON --而且您没有编写测试;)
{
"fname": "john",
"lname": "doe"
}请在http://ideone.com/5IW1Ef上查看演示
您所使用的类做了一些神奇的事情:
$this->response($this->db->get('books')->result(), 200);
并根据URL上指定的格式,将响应数据转换为JSON。您不必执行JSON编码。
请阅读这里提供的示例https://github.com/chriskacerguis/codeigniter-restserver#responses
$fullname = array("fname"=>"john", "lname"=>"doe");
$this->response($fullname, 200);http://code.tutsplus.com/tutorials/working-with-restful-services-in-codeigniter-2--net-8814
发布于 2015-01-16 06:12:05
你必须去掉斜杠,使用这个斜杠(json_encode($fullname))。全文如下:
public function fullname_get()
{
$fullname = array("fname"=>"john", "lname"=>"doe");
$data["json"] = stripslashes(json_encode($fullname));
$this->response($data["json"], 200);
}我希望这能解决你的问题。
https://stackoverflow.com/questions/27972580
复制相似问题