0

I've got a problem regarding JSON.

The set-up is very simple. I'm using the Lumen framework and within the 'Controller' class I've got a function which gets some information from the database which I want to use in another controller called 'GameController'. Here's the code that matters:

class GameController extends Controller
{
...
public function index(Request $request) 
{       
       $jsonGame = $this->getItem('games', 'gametitle', $request->input('title')); 
}
...
}



class Controller extends BaseController
{
public function getItem($table, $column, $item)
{
    try{
        $result = app('db')->table($table)
                    ->where($column, $item)
                    ->take(1)
                    ->get();

        if(count($result) != 0) {
            return Response::json($result);
        }

        return Response::notFound('Item not found');
    } 
    catch(QueryException $e) {
       ...
    }
}
}

Now the problem is that, as I try to decode the json in the variable $jsonGame and check if there are no errors, there's always the errorcode 4 and the decoding failed. I've checked it with the following:

    if (json_last_error() === JSON_ERROR_NONE) { 
         echo 'json';
     } else { 
         echo json_last_error(); // gives error code 4
     }  

So then I began checking the json manually, and this is what it outputs. The output of echo $jsonGame:

HTTP/1.0 200 OK Cache-Control: no-cache, private Content-Type: application/json Date: Mon, 24 Jul 2017 12:58:03 GMT

[{"gameid":2,"gametitle":"rocket league","gamedescription":"","created_at":"2017-07-24 07:02:43","updated_at":"2017-07-24 07:02:43"}]

The output of return $jsonGame:

[ { "gameid": 2, "gametitle": "rocket league", "gamedescription": "", "created_at": "2017-07-24 07:02:43", "updated_at": "2017-07-24 07:02:43" } ]

So as you can see the above seems to be valid JSON, and I'm melting my brain over this. Why isn't decoding working? Why does it give errorcode 4?

halfer
  • 19,824
  • 17
  • 99
  • 186
Serellyn
  • 405
  • 1
  • 9
  • 26
  • Please check this `$gameArray =json_decode($jsonGame , TRUE);` what its return in $gameArray? – Jaykumar Gondaliya Jul 24 '17 at 13:36
  • one more thing set this `header('Content-type:application/json;charset=utf-8');` – Jaykumar Gondaliya Jul 24 '17 at 13:51
  • I have no idea of php but your json response is a list that contains one object. Are you expecting this on your decoding? Because if you are expecting only the object (game), this might be the cause of your problem – Carlos Jul 24 '17 at 13:57
  • If the above comments don't work, you might try checking your server logs for any errors or warnings. – rideron89 Jul 24 '17 at 14:01
  • @JaykumarGondaliya, thank you for your response. The $gameArray as suggested returns completely nothing, totally empty. But after I set the 'header' the header information in the echo I got before is gone. But, still, the decoding seems to fail. – Serellyn Jul 24 '17 at 14:48
  • @Carlos it also happens on a 'normal' json string such as: { "title": "Resource not found", "detail": "Item not found", "code": 404 } Still fails decoding. – Serellyn Jul 24 '17 at 14:49
  • @rideron89 I've looked through them, there's nothing :( – Serellyn Jul 24 '17 at 14:49

0 Answers0