/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package aaa;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;
import testetabela.CadMot;
/**
*
* @author Admin
*/
public class ClasseDeObjetos {
public static void main(String[] args) {
Gson gson = new Gson();
List<CadMot> lista = new ArrayList<>();
CadMot cadmot1 = new CadMot();
cadmot1.setMotCodigo(1);
cadmot1.setMotDescricao("Descricao 0001");
lista.add(cadmot1);
CadMot cadmot2 = new CadMot();
cadmot2.setMotCodigo(2);
cadmot2.setMotDescricao("Descricao 0002");
lista.add(cadmot2);
String s = gson.toJson(lista);
System.out.println(s);
List<CadMot> lista2 = gson.fromJson(s, ArrayList.class);
for (CadMot cadmot3:lista2) {
System.out.println(cadmot3.getMotCodigo());
System.out.println(cadmot3.getMotDescricao());
}
}
}
me da o seguinte retorno:
[{"MotDescricao":"Descricao 0001","MotCodigo":1},{"MotDescricao":"Descricao 0002","MotCodigo":2}]
Exception in thread "main" java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to testetabela.CadMot
at aaa.ClasseDeObjetos.main(ClasseDeObjetos.java:41)
C:\Users\Admin\AppData\Local\NetBeans\Cache\8.2rc\executor-snippets\run.xml:53: Java returned: 1
FALHA NA CONSTRUÇÃO (tempo total: 0 segundos)