json Object in array this with java error

Asked

Viewed 52 times

0

I have a Webservice Rest with two classes: Pedido and itensPedido.

In the pedido the Insert works, but when it goes to the itensPedido that I use a JSON Array with multiple items returns the error:

Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path.

I’ll tell you what: I open the Postman, Gero the JSON Array by method post and sending. Here returns the error.

Someone can guide me?

My code:

@POST
@Path("salvaritenspedido")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public String salvarItensPedido(String itensPedido){
    ItensPedido ip1 = g.fromJson(itensPedido, ItensPedido.class);
    ItensPedido ip2 = new ItensPedidoDao().getItensPedidos(ip1.getIdPedido(), ip1.getIdVendedor(), ip1.getIdCliente(), ip1.getIdProduto());
        if(ip2 == null){
            System.out.println("SALVAR");
            //System.out.println(Arrays.toString(ip1));

        } else {
            System.out.println("ATUALIZAR");
            //System.out.println(Arrays.toString(ip1));

        }
        return itensPedido;
}
  • 1

    What the message says is . "I expected the character { representing a single object in JSON but found [ representing a Array JSON. Add if you can your JSON as well. It’s like turning an Itensrequested Array into a single Itensrequested object

  • @lazarojvt, put the contents of the string in the "itensPedido" parameter along with the question.

  • Was any of the answer helpful? Don’t forget to choose one and mark it so it can be used if someone has a similar question!

1 answer

0

If you’re getting one array ([]) you should turn into a list:

Type tipo = new TypeToken<ArrayList<ItensPedido>>(){}.getType();
List<ItensPedido> lista = (new Gson()).fromJson(jsonArray, tipo);

Browser other questions tagged

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