从CI REST API返回的json失败的jsonlint测试 [英] json returned from CI REST API is failing jsonlint tests

查看:226
本文介绍了从CI REST API返回的json失败的jsonlint测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在codeigniter REST应用程式中建立以下程式码(建立使用: https://github.com/ chriskacerguis / codeigniter-restserver

I have the following code in a codeigniter REST app (built using: 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看起来像这样: p>

When I call the API, this return json that looks like this:

{\"fname\":\"john\",\"lname\":\"doe\"}

上述json字符串失败 http://jsonlint.com/ 测试,因为转义字符\。
只是想知道我该如何解决这个问题?
我正在构建一个REST api,它应该返回json ...我必须确保它是合法的json。

The above json string fails http://jsonlint.com/ test because of the escape character "\". Just wondering how I can work around this? I'm building a REST api that is supposed to return json ... and I have to make sure it's legit json.

谢谢。

推荐答案

尝试此

 $fullname = array("fname"=>"john", "lname"=>"doe");
 $this->response($fullname, 200);//it sends data json format. You don't need to json encode it

你得到了响应,因为你的数据是json编码两次

You got that response because your data is json encoded twice

这篇关于从CI REST API返回的json失败的jsonlint测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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