0

Hi there I have a small problem i can't figure out. Im writing a RPC service using ZendFramework and Apigility. The response must be a json array. Next comes all the content negotiation code.

'controllers' => array(
        'NmdaWebApi\\V1\\Rpc\\Hola\\Controller' => 'Json',

'accept_whitelist' => array(
        'NmdaWebApi\\V1\\Rpc\\Hola\\Controller' => array(
            0 => 'application/vnd.nmda-web-api.v1+json',
            1 => 'application/json',
            2 => 'application/*+json',
        ),
'content_type_whitelist' => array(
        'NmdaWebApi\\V1\\Rpc\\Hola\\Controller' => array(
            0 => 'application/json',
        ),

This is how I have the controller

class HolaController extends AbstractActionController{
        public function holaAction(){
                return array(1,2,3,4,5);}}

And here is the returned json.

{"0":1,"1":2,"2":3,"3":4,"4":5}

I'm getting this list and a I want an array. Here is another example.

return array(1,2,3,4,array(5,6,7));
{"0":1,"1":2,"2":3,"3":4,"4":[5,6,7]}

Can someone explain me how to avoid this?

Edit_1

I figured out a workaround. Using HalJson fixes the problem. But i still cant understand why it doesnt work with normal Json.

'controllers' => array(
        'NmdaWebApi\\V1\\Rpc\\Hola\\Controller' => 'HalJson',
2
  • if it suits you try to return array('ids' => array(1,2,3,4,5)); than you will have your array but in key "ids" Commented Mar 2, 2015 at 22:14
  • mmm not rly what I'm looking for. Commented Mar 2, 2015 at 22:18

1 Answer 1

2

In your action you must return a JsonModel. To do that, first create a 'strategies' key under your 'view_manager' section in your module.config.php:

'view_manager' => array(
    'strategies'                => array(
        'ViewJsonStrategy',
    ),
),

Now you can return a JsonModel in your action:

$data = array(1, 2, 3, 4, 5);

return new JsonModel([
    'data' => $data
]);
Sign up to request clarification or add additional context in comments.

2 Comments

I'm going to give it a try.
Returning JsonModel from my Controller class work. I just use it like this. return new JsonModel($data);.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.