Create Json using Flight php

1k views Asked by At

I have a problem where I have to create a JSON using Flight::json. I have an array, called $data, that contains some elements like

  $data[] = array('id'=>$temp,'type'=>'remote','url'=>$path);

where $id and $path have different values like this:

[id] => http://desktop-pqb3a65:8080/marmotta/resource/22086372-476f-4974-b538-64019ab678b3
[url] => D:\Software\Marmotta\marmotta-home\resources\1d\4d\ea\1d4dea13-f8e6-4cf0-b96a-f88b08efda2b 

When I try to convert that in a JSON using:

Flight::json($data);

my PHP page returns me this format instead:

{"id":"http:\/\/desktop-pqb3a65:8080\/marmotta\/resource\/22086372-476f-4974-b538-64019ab678b3","type":"remote","url":"D:\\Software\\Marmotta\\marmotta-home\\resources\\1d\\4d\\ea\\1d4dea13-f8e6-4cf0-b96a-f88b08efda2b"}

I read the documentation and I tried to convert using another function:

Flight::json($data, $code = 200, $encode = false, $charset = 'utf-8');

but it return an error like:

500 Internal Server Error
Array to string conversion (8)

So can you help me to convert $data without this type of error? I must use Flight to convert my array.

Thanks all for help!

EDIT

I solve my problem creating a function like this:

Flight::map('jsonc', function($obj, $status = 200) {
Flight::response()
    ->status($status)
    ->header('Content-Type', 'application/javascript')
    ->write(utf8_decode(json_encode($obj, JSON_UNESCAPED_SLASHES)))
    ->send();
  });
1

There are 1 answers

0
josevh On

If you want unescaped slashes I believe you can pass that as the 5th function parameter named $option, see source.

public function _json(
        $data,
        $code = 200,
        $encode = true,
        $charset = 'utf-8',
        $option = 0
    )