Commit 5bb99998 authored by Dr. Daniel Diaz Sánchez's avatar Dr. Daniel Diaz Sánchez
Browse files

Update README.md

parent 583a7983
...@@ -261,14 +261,21 @@ public class TestJSON { ...@@ -261,14 +261,21 @@ public class TestJSON {
public TestJSON() { public TestJSON() {
// Crear un JSON // Crear un JSON
Collection<Employee> colection = new ArrayList<Employee>(); Collection<Employee> colection = new ArrayList<Employee>();
Employee e1 = new Employee("John", "Doe"); Employee e1 = new Employee();
Employee e2 = new Employee("Anna", "Smith"); e1.firstName="John";
Employee e3 = new Employee("Peter", "Jones"); e1.lastName= "Doe";
Employee e2 = new Employee();
e1.firstName="Anna";
e1.lastName= "Smith";
Employee e3 = new Employee();
e1.firstName="Peter";
e1.lastName= "Jones";
colection.add(e1); colection.add(e1);
colection.add(e2); colection.add(e2);
colection.add(e3); colection.add(e3);
Gson gson = new Gson(); Gson gson = new Gson();
String json_represntation = gson.toJson(colection); String json_represntation = gson.toJson(colection);
System.out.print("Representación : \n" + json_represntation);
System.out.print(json_represntation); System.out.print(json_represntation);
// al revés, pero más complicado // al revés, pero más complicado
// procesar un mensaje recibido con JSON, en el que no solo hay tipos Empoyee // procesar un mensaje recibido con JSON, en el que no solo hay tipos Empoyee
...@@ -292,6 +299,7 @@ public class TestJSON { ...@@ -292,6 +299,7 @@ public class TestJSON {
String json_recibido2 = "[{\"firstName\":\"John\", \"lastName\":\"Doe\"},{\"firstName\":\"Anna\", \"lastName\":\"Smith\"},{\"firstName\":\"Peter\", \"lastName\": \"Jones\"}]"; String json_recibido2 = "[{\"firstName\":\"John\", \"lastName\":\"Doe\"},{\"firstName\":\"Anna\", \"lastName\":\"Smith\"},{\"firstName\":\"Peter\", \"lastName\": \"Jones\"}]";
HashMap<String,Object> mensaje = gson.fromJson(json_recibido, jType); HashMap<String,Object> mensaje = gson.fromJson(json_recibido, jType);
System.out.println(" ----- ");
System.out.println(mensaje.get("mensaje")); System.out.println(mensaje.get("mensaje"));
System.out.println(mensaje.get("id")); System.out.println(mensaje.get("id"));
...@@ -307,11 +315,12 @@ public class TestJSON { ...@@ -307,11 +315,12 @@ public class TestJSON {
} }
public static void main(String args[]) { public static void main(String args[]) {
new TestJSON(); new TestJSON();
} }
} }
``` ```
Crea la clase Employee.java: Crea la clase Employee.java:
...@@ -322,10 +331,9 @@ public class Employee { ...@@ -322,10 +331,9 @@ public class Employee {
public String firstName; public String firstName;
public String lastName; public String lastName;
public Employee(String firstName, String lastName) {
super(); public Employee() {
this.firstName = firstName; // TODO Auto-generated constructor stub
this.lastName = lastName;
} }
public String toString() { public String toString() {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment