我有一個用戶定義的類,說轉換用戶自定義對象到字符串對象
import java.util.Calendar;
public class Employee{
private String name;
private int age;
private Calendar dob;
private Address address;
private boolean married;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Calendar getDob() {
return dob;
}
public void setDob(Calendar dob) {
this.dob = dob;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public boolean isMarried() {
return married;
}
public void setMarried(boolean married) {
this.married = married;
}
}
class Address{
private int doorNo;
private String streetName;
private String city;
public int getDoorNo() {
return doorNo;
}
public void setDoorNo(int doorNo) {
this.doorNo = doorNo;
}
public String getStreetName() {
return streetName;
}
public void setStreetName(String streetName) {
this.streetName = streetName;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
我創建員工的對象,並與setter方法填充它。我必須將上述對象表示爲字符串(加密或人類可讀)並解析回來以獲得類似的對象。實際上,我想將相當於Java對象的字符串保存在一個文件中,並讀回它們以獲得一個java對象。我知道我們有寫作的對象,但他們對編輯很敏感。我寧願如果一個Java對象可以轉換爲人類可讀形式的字符串。謝謝。
你有什麼需要的是系列化.. – 2012-01-02 10:55:00
JSON是去給你,請檢查下面的示例代碼我的回答方式。 – anubhava 2012-01-02 11:12:43
謝謝大家!我已經提出了所有的答案!感謝您的熱烈響應。 – Ahamed 2012-01-02 13:30:12