문제

I am trying to create an api with Recess and I have a question about its JsonView. Currently, if I do a GET request on, for example, /users/1 (which routes to a function that gets all the details for the user with id 1 and responds with Json), I get the following:

{"users":{"id":"1","username":null,"password":null,"datejoined":false}}

How can I make it so that I get the following instead:

{"id":"1","username":null,"password":null,"datejoined":false}

That is, I don't want all the details wrapped inside "users":{}.

도움이 되었습니까?

해결책

By default, Recess's JsonView responds with the properties of your controller. So your $users property is getting directly encoded into JSON.

You can override this by returning a custom response object:

return new OkResponse($this->request, (array)$this->users);

다른 팁

Not sure about recess specifically, but if you are using JsonView method/function with an input parameter (array) $result, then changing $result to $result['users'] may give you the answer you are looking for.

For example using plain PHP:

first object: echo json_encode($result);
second object: echo json_encode($result['users']);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top