I'm having some problems with JSON in my application and wanted to see if you guys can help me, please.
The situation is as follows:
I have a method in my controller that looks for some items:
@RequestMapping(value = "/buscaTodosCardapios", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public String buscaTodosCardapios(Usuario usuario, Model model){
JSONObject retorno = new JSONObject();
try{
retorno.put("data", cardapioRepository.findBySubmenuIsNull());
}catch (Exception ex){
retorno.put("situacao", "ERRO");
retorno.put("mensagem", "Falha ao iniciar produção!");
}
return retorno.toString();
}
A JS file that takes these items:
function buscaTodosCardapios(){
$.ajax({
url: "buscaTodosCardapios",
type: 'GET',
dataType: 'json',
cache: false,
success: updateCardapios
});
}
function updateCardapios(data){
console.log(data);
console.log(data.cardapioId);
}
Only, as you can see, the second console.log, does not return anything and I can not seem to get it to pick up some element from within the date. Can anyone help me with this?
-EDIT- Solution for anyone who needs it, just move to:
console.log(data.data[0].cardapioId);