Código da classe PhoneNumber
public class PhoneNumber {
private int code;
private String number;
public PhoneNumber() {
}
public PhoneNumber(int code, String number) {
this.code = code;
this.number = number;
}
//Métodos GET e SET.....
}
Código da classe Person
import java.util.Date;
import java.util.List;
public class Person {
private String firstName;
private String lastname;
private int idade;
private boolean casado;
private Boolean solteiro;
private Long altura;
private Integer numero;
private PhoneNumber phone;
private PhoneNumber fax;
private Date data;
private List phones;
private PhoneNumber[] arrayPhones;
public Person() {}
public Person(String fn, String ln) {
this.firstName = fn;
this.lastname = ln;
}
// Métodos GET e SET........
}
Código da classe TesteJson
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import json.serializer.JSONSerializer;
public class TesteJson {
public static void main(String[] args) {
Person joe = new Person("Joe", "Wayne");
PhoneNumber phoneJoe = new PhoneNumber(321, "5565-987");
PhoneNumber fax = new PhoneNumber(123, "9999-999");
PhoneNumber[] arrayPhones = new PhoneNumber[2];
arrayPhones[0] = phoneJoe;
arrayPhones[0] = fax;
List phones = new ArrayList();
phones.add(phoneJoe);
phones.add(fax);
joe.setPhone(phoneJoe);
joe.setFax(fax);
joe.setData(Calendar.getInstance().getTime());
joe.setIdade(12);
joe.setAltura(new Long(120));
joe.setArrayPhones(arrayPhones);
joe.setPhones(phones);
joe.setAltura(new Long(120));
joe.setCasado(true);
joe.setSolteiro(new Boolean(false));
joe.setNumero(new Integer(53));
Person moe = new Person("Bart", "Simpson");
PhoneNumber phone = new PhoneNumber(321, "5565-987");
moe.setPhone(phone);
moe.setFax(new PhoneNumber(321, "9999-999"));
moe.setData(Calendar.getInstance().getTime());
List collection = new ArrayList();
collection.add(joe);
collection.add(moe);
JSONSerializer serial = new JSONSerializer();
String strJoe = serial.serialize(joe);
System.out.println(strJoe);
serial = new JSONSerializer();
strJoe = serial.serialize(collection);
System.out.println(strJoe);
}
}
Nenhum comentário:
Postar um comentário