json returned from CI REST API is failing jsonlint tests

后端 未结 3 632
天涯浪人
天涯浪人 2021-01-28 08:05

I have the following code in a codeigniter REST app (built using: https://github.com/chriskacerguis/codeigniter-restserver)

public function fullname_get()
{
   $         


        
相关标签:
3条回答
  • 2021-01-28 08:17

    Try this

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

    You got that response because your data is json encoded twice

    0 讨论(0)
  • 2021-01-28 08:18

    It is legit JSON - and you didn't write a test ;)

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

    Please see the demo over at http://ideone.com/5IW1Ef


    The class you are using does magical things:

    $this->response($this->db->get('books')->result(), 200);

    and based on the format specified on the URL the response data is converted to JSON. You don't have to do the JSON encoding.

    Please read the examples provides here 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

    0 讨论(0)
  • 2021-01-28 08:42

    You have to strip slashes, use this stripslashes(json_encode($fullname)). Full code mention below:

    public function fullname_get()
    {
       $fullname = array("fname"=>"john", "lname"=>"doe");
       $data["json"] = stripslashes(json_encode($fullname));
       $this->response($data["json"], 200);
    }
    

    I hope this will solve your issue.

    0 讨论(0)
提交回复
热议问题