Descobri o GSON do Google, porque tinha algum entendimento do protocolbuf antes e tinha alguma curiosidade, comecei a usar o GSON.
Página inicial do Github: https://github.com/google/gson
Após a comparação, o GSON e outras bibliotecas de classes Java JSON existentes têm as maiores diferenças. O GSON precisa ser serializado para obter classes de entidade sem usar a anotação para identificar campos que precisam ser serializados. Ao mesmo tempo, o GSON pode configurar de maneira flexível campos que precisam ser serializados usando a anotação.
É muito simples de converter lista ou mapa em JSON:
public string getjsondata (list <?> list) {gson gson = new gson (); String jSonstring = gson.tojson (list); retornar JSonstring;}Teremos exemplos detalhados abaixo.
Exemplo
Conversão de objetos simples e conversão de listar com genéricos:
Classe de entidade:
Public Class Student {private Int ID; nome de string privado; aniversário privado; public int getId () {return id; } public void setId (int id) {this.id = id; } public string getName () {return name; } public void setName (nome da string) {this.name = name; } public data getBirthday () {return Birthday; } public void setbirthday (data de aniversário) {this.birthday = aniversário; } @Override public string tostring () {return "Student [aniversário =" + aniversário + ", id =" + id + ", name =" + name + "]"; }}Classe de teste:
importar java.util.arraylist; importar java.util.date; importar java.util.list; importar com.google.gson.gson; importar com.google.gson.reflect.typetken; classe pública gsontest1 {public static void main (string [] args) {gson gson = new gson (); Aluno aluno1 = novo aluno (); estudante1.setId (1); Student1.setName ("Li Kun"); estudante1.setBirthday (new Date ()); // /////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////// System.out.println ("JSON to Simple Bean ===" + Student); // resultado: // bean simples para json === {"id": 1, "name": "li kun", "aniversário": "22 de junho de 2012 8:27:52"} // json para bean simples === aluno [aniversário = fri 22 de junho 08:27:52 CST 2012, id = 1, // Nome = Li Know = Li Kun. //////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////// estudante2 = novo aluno (); estudante2.setId (2); Student2.SetName ("Cao Guisheng"); Student2.SetBirthday (new Date ()); Aluno estudante3 = novo aluno (); Student3.SetId (3); Student3.SetName ("Liu Bo"); estudante3.setBirthday (new Date ()); List <very Student> list = new ArrayList <very Student> (); list.add (Student1); list.add (Student2); list.add (Student3); System.out.println ("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Result: // List with generics is converted to json==[{"id":1,"name":"Li Kun","birthDay":"Jun 22, 2012 8:28:52 AM"},{"id":2,"name":"Cao Guisheng","birthDay":"Jun 22, 2012 8:28:52 AM"},{"id":3,"name":"Liu Bo "," Aniversário ":" Jun 22, 2012 8:28:52 "}] // estudante [aniversário = sexta Nome = Liu bo]}}Resultados da execução:
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- PM "}, {" id ": 2," Nome ":" Cao Guisheng "," Aniversário ":" Jun 22, 2012 21:10:31 "}, {" id ": 3," Nome ":" Liu bo "," Aniversário ":" Jun 22, 2012, 9:10:31 "}] estudante [Id [Fri Fri 22 22 22 22 22 22: 22 21:10:31 CST 2012, ID = 2, Nome = Cao Guisheng] Student [Aniversário = sex 22 21:10:31 CST 2012, ID = 3, Nome = Liu Bo]