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 {
public TestJSON() {
// Crear un JSON
Collection<Employee> colection = new ArrayList<Employee>();
Employee e1 = new Employee("John", "Doe");
Employee e2 = new Employee("Anna", "Smith");
Employee e3 = new Employee("Peter", "Jones");
Employee e1 = new Employee();
e1.firstName="John";
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(e2);
colection.add(e3);
Gson gson = new Gson();
String json_represntation = gson.toJson(colection);
System.out.print("Representación : \n" + json_represntation);
System.out.print(json_represntation);
// al revés, pero más complicado
// procesar un mensaje recibido con JSON, en el que no solo hay tipos Empoyee
......@@ -292,6 +299,7 @@ public class TestJSON {
String json_recibido2 = "[{\"firstName\":\"John\", \"lastName\":\"Doe\"},{\"firstName\":\"Anna\", \"lastName\":\"Smith\"},{\"firstName\":\"Peter\", \"lastName\": \"Jones\"}]";
HashMap<String,Object> mensaje = gson.fromJson(json_recibido, jType);
System.out.println(" ----- ");
System.out.println(mensaje.get("mensaje"));
System.out.println(mensaje.get("id"));
......@@ -307,11 +315,12 @@ public class TestJSON {
}
public static void main(String args[]) {
new TestJSON();
}
public static void main(String args[]) {
new TestJSON();
}
}
```
Crea la clase Employee.java:
......@@ -322,10 +331,9 @@ public class Employee {
public String firstName;
public String lastName;
public Employee(String firstName, String lastName) {
super();
this.firstName = firstName;
this.lastName = lastName;
public Employee() {
// TODO Auto-generated constructor stub
}
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