Tôi đã lên kế hoạch sử dụng SimpleXML cho nhu cầu tuần tự hóa của mình, nhưng tôi nghĩ tôi nên thử JSON để tìm hiểu điều gì đó mới.
Đây là mã tôi đã sử dụng để thử tuần tự hóa một ArrayList POJO bằng Gson 1.7.1.
Lưu ý: Tôi đã xóa trình đọc/ghi chuỗi "s" để đơn giản hóa mã.
kiểm tra gói;
nhập java.io.IOException;
nhập java.util.ArrayList;
nhập java.util.Collections;
nhập java.util.List;
nhập com.google.gson.Gson;
lớp công khai TestGsonSerialDeserialList {
public static void main(String[] args) ném IOException{
Gson gson = Gson mới();
//Tạo Serial
Chuỗi s;
Danh sách danh sách = Collections.synchronizedList(mới ArrayList() );
list.add(đối tượng TestObject mới());
list.add(đối tượng TestObject mới());
s = gson.toJson(danh sách, ArrayList.class);
Hệ thống.out.println(s);
//Ăn Serial
Danh sách list2 = Collections.synchronizedList(gson.fromJson(s, ArrayList.class) );
System.out.println(list2.get(0) );
System.out.println(list2.get(1) );
}
}
Đây là đầu ra tôi nhận được:
[{"objectID":1,"i1":12345,"name://abcdefg","s":["a","b","c"]},{"objectID":2,"i1 ":12345,"name://abcdefg","s":["a","b","c"]}]
java.lang.Object@5c74c3aa
java.lang.Object@75d9fd51
Đối với mắt người mới của tôi, điều này có vẻ đúng. Tuy nhiên, danh sách các đối tượng DeSerialized chứa đối tượng cơ sở thay vì TestObject được tuần tự hóa I. Bất cứ ai có thể giải thích cho tôi những gì, nếu có, tôi có thể làm gì để thực hiện công việc này?
biên tập:
Kiểm tra đã sửa: Cảm ơn ColinD
kiểm tra gói;
nhập java.io.FileInputStream;
nhập java.io.FileOutputStream;
nhập java.io.IOException;
nhập java.io.InputStreamReader;
nhập java.io.OutputStreamWriter;
nhập java.io.Reader;
nhập java.io.Writer;
nhập java.lang.reflect.Type;
nhập java.util.ArrayList;
nhập java.util.Collections;
nhập java.util.List;
nhập com.google.gson.Gson;
nhập com.google.gson.reflect.TypeToken;
lớp công khai TestGsonSerialDeserialList {
public static void main(String[] args) ném IOException{
System.out.println("--- Đã bắt đầu tuần tự hóa / Hủy tuần tự hóa ---");
Chuỗi fileName = "json\\testList.json";
Gson gson = Gson mới();
Kiểu listOfTestObject = new TypeToken<>>(){}.getType();
//Tạo Serial
Người viết osWriter = new OutputStreamWriter( new FileOutputStream(fileName));
Danh sách danh sách = Collections.synchronizedList(mới ArrayList() );
list.add(đối tượng TestObject mới());
list.add(đối tượng TestObject mới());
list.add(đối tượng TestObject mới());
list.add(đối tượng TestObject mới());
gson.toJson(danh sách, osWriter);
osWriter. close();
//Ăn Serial
Người đọc isReader = new InputStreamReader( new FileInputStream((fileName) ) );
Danh sách list2 = Collections.synchronizedList(
(Danh sách)gson.fromJson(isReader, listOfTestObject)
);
isReader.close();
System.out.println(list2.get(0) );
System.out.println(list2.get(1) );
System.out.println(list2.get(2) );
System.out.println(list2.get(3) );
System.out.println("--- Đã kết thúc tuần tự hóa / Hủy tuần tự hóa ---");
}
}
Đầu ra:
--- Đã bắt đầu tuần tự hóa / hủy tuần tự hóa ---
ID#: 1, i1: 12345, name: abcdefg, s[]: [Ljava.lang.String;@95c083
ID#: 2, i1: 12345, name: abcdefg, s[]: [Ljava.lang.String;@6791d8c1
ID#: 3, i1: 12345, name: abcdefg, s[]: [Ljava.lang.String;@182d9c06
ID#: 4, i1: 12345, name: abcdefg, s[]: [Ljava.lang.String;@5a5e5a50
--- Serialize / Deserialize Ended ---
EDIT2:
老实说,我不知道为什么,但是当我用 ArrayList 替换嵌入在我的 TestObject 中的简单 String[] 时,它开始正确序列化。
--- Đã bắt đầu tuần tự hóa / hủy tuần tự hóa ---
ID#: 1, i1: 12345, name: abcdefg, s[]: [a, b, c]
ID#: 2, i1: 12345, name: abcdefg, s[]: [a, b, c]
ID#: 3, i1: 12345, name: abcdefg, s[]: [a, b, c]
ID#: 4, i1: 12345, name: abcdefg, s[]: [a, b, c]
--- Serialize / Deserialize Ended ---
Tôi là một lập trình viên xuất sắc, rất giỏi!