简体   繁体   English

如何防止laravel json响应添加反斜杠

[英]How to prevent laravel json response from adding backslashes

I'm having trouble parsing the json response from my API via javascript because of the backslashes being added returned by laravel.由于 laravel 添加了反斜杠,我无法通过 javascript 解析来自我的 API 的 json 响应。 I noticed that if it was a model being converted to json, laravel has no problem sending it back with no backslashes.我注意到如果它是一个被转换为 json 的模型,laravel 将它发送回没有反斜杠没有问题。 However, I'm using guzzle to access an API, and I have to add that API's response to my own custom response with some other data.但是,我正在使用 guzzle 来访问 API,并且我必须将该 API 的响应添加到我自己的自定义响应以及其他一些数据中。

I've logged my response data before being sent back via response()->json() and here's the content:在通过response()->json()发回之前,我已经记录了我的响应数据,这是内容:

array (
  'data' =>
  array (
    'checkout' => '{"timestamp":1600480972698,"status":200,"message":"Request processed successfully","path":"/checkout","data":{"transaction_id":"xxxxx","status_description":"Transaction ready for payment","payment_url":"http://abcdefg.com/abcdefg'
    'transaction' => '{"data":{"transaction":{"billable_id":5,"amount_due":2,"transaction_fee":0.06000000000000005,"amount_paid":0,"reference_id":null,"reference_no":"PTX-0920UI22QJU2","payment_type_id":9,"transaction_status_id":1,"updated_at":"2020-09-19T02:03:09.0000$
  ),
  'status_code' => 200,
)

But what's actually returned by the response()->json($responseData) is:但是response()->json($responseData)实际返回的是:

{
   "data":{
      "checkout":"{\"timestamp\":1600480972698,\"status\":200,\"message\":\"Request processed successfully\",\"path\":\"\/checkout\",\"data\":{\"transaction_id\":\"200919020252BEZ5305\",\"status_description\":\"Transaction ready for payment\",\"payment_url\":\"http:\/\/abcdefg.com\/abcdefg\/1.2.1\/index.html#\/confirm?bizNo=20200919121212800110170567600273184&timestamp=1600480989712&sign=ChbtFZnRH5Mkqlou2BrZZ6TSvhrChmaTUPJnvWok14bVgr1lqMp46mOHoMm%2Fcy%2FhbfUzMoKJ0aG4%2BEjN%2BpFJaW5NchzV6WaZmur1YjFmPzaHVom74kGZcsWeJymgU%2BGmfnyrRaYP971%2BBl4Jqv6NX4gpGsIhzRSsiZIK5cxmk5DHIvn4MTA2834gS3%2FLTfWiQYHmmhhux3FL7eC72IgjW%2BC87pa2GC6XP0BCbgRpyMNUdD%2F5hEoHVKiWdNi8K9WyFZyxOyj%2B73gY%2F4XUJf6wyCLUxg8l6L4xnE0meBOguB8Q2qhS33NUvqXQowsJTn%2BGGEBeu9b4JgKf2cIaPlxE7A%3D%3D&merchantName=APNorderAmount=2.00\",\"status_code\":\"OK.01.00\"}}",
      "transaction":"{\"data\":{\"transaction\":{\"billable_id\":5,\"amount_due\":2,\"transaction_fee\":0.06000000000000005,\"amount_paid\":0,\"reference_id\":null,\"reference_no\":\"PTX-0920UI22QJU2\",\"payment_type_id\":9,\"transaction_status_id\":1,\"updated_at\":\"2020-09-19T02:03:09.000000Z\",\"created_at\":\"2020-09-19T02:03:09.000000Z\",\"id\":77}},\"message\":\"Transaction successfully created.\",\"status_code\":200}"
   },
   "status_code":200
}

Note: There might be some unwanted characters in the json above since I only copied these from the laravel.log via nano/terminal on my elastic beanstalk server due to constraints that I can only access the API from my server since the IP has to be whitelisted.注意:上面的 json 中可能有一些不需要的字符,因为我只能通过弹性 beantalk 服务器上的 nano/终端从 laravel.log 复制这些字符,因为限制我只能从我的服务器访问 API,因为 IP 必须列入白名单。

Is these any way to return the data on my second code snippet without the backslashes?这些有什么方法可以在没有反斜杠的情况下返回我的第二个代码片段上的数据吗? I returned it with the exact code return response()->json($response)->setStatusCode($response['status_code']);我用确切的代码返回了它return response()->json($response)->setStatusCode($response['status_code']);

try using尝试使用

return response()->json($response,$response['status_code'],[],JSON_UNESCAPED_SLASHES);

the json() method (from interface ResponseFactory) take the forth parameter as json option, json() 方法(来自接口 ResponseFactory)将第四个参数作为 json 选项,

JSON_UNESCAPED_SLASHES indicate that the encoding op will not escape slashes JSON_UNESCAPED_SLASHES表示编码操作不会转义斜杠

in your model try to add this attribute.在您的模型中尝试添加此属性。 this is just example code.这只是示例代码。

public function setCheckoutAttribute($value)
    {
        $this->attributes['checkout'] = json_encode($value);
    }
  
    public function getCheckoutAttribute($value)
    {
        return $this->attributes['checkout'] = json_decode($value);
    }

public function setTransactionAttribute($value)
        {
            $this->attributes['transaction'] = json_encode($value);
        }
     
        public function getTransactionAttribute($value)
        {
            return $this->attributes['transaction'] = json_decode($value);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM