Remove "[ ]" brackets from json return

Asked

Viewed 679 times

0

I have an api that returns me an array in JSON, as follows:

[{"Pessoa_contato":"EUZEBIO"}]

But I’m not getting back without these brackets, as my application needs:

{"Pessoa_contato":"EUZEBIO"}

This is the script of this return:

try {
    if (class_exists($classe)) {
        if (method_exists($classe, $metodo)) {
            $retorno = call_user_func_array(array(new $classe, $metodo), 
                $parametros);
            return json_encode(array( $retorno));
        } else {
            return json_encode(array('Método inexistente!'));
        }
    } else {
        return json_encode(array('Classe inexistente!'));
    }   
}
  • 2

    only do so in return return json_encode($retorno); I think that’s it, I have no more information so it’s in the chutometro

1 answer

2

Dear a solution suggestion.

First a json must have key and value "a":"b"

soon:

  return json_encode(array('Método inexistente!'));
    }
} else {
    return json_encode(array('Classe inexistente!'));

will not generate nor an array even valid a json valid

About the problem itself, remove the method array() seems to be enough

hugs.

  • In short, brackets mean array, you are purposefully encapsulating the $return within an array...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.