依赖jar包
<dependency>
<groupId>org.sction</groupId>
<artifactId>phprpc</artifactId>
<version>3.0.2</version>
</dependency>
序列化:
PHPSerializer phpSerializer = new PHPSerializer();
List<HashMap> list = new ArrayList<>();
HashMap hashMap1 = new HashMap<>();
hashMap1.put("id", "111");
hashMap1.put("name", "关羽");
list.add(hashMap1);
HashMap hashMap2 = new HashMap<>();
hashMap2.put("id", "222");
hashMap2.put("name", "张飞");
list.add(hashMap2);
try {
byte[] serialize = phpSerializer.serialize(list);
String res = new String(serialize);
System.out.println(res);
} catch (Exception e) {
e.printStackTrace();
}
运行结果:
?反序列化:
PHPSerializer phpSerializer = new PHPSerializer();
try {
String seria = "a:2:{i:0;a:2:{s:4:\"name\";s:6:\"关羽\";s:2:\"id\";s:3:\"111\";}i:1;a:2:{s:4:\"name\";s:6:\"张飞\";s:2:\"id\";s:3:\"222\";}}";
AssocArray assocArray = (AssocArray) phpSerializer.unserialize(seria.getBytes());
List<TestA> list = new ArrayList<>();
for(int i = 0; i < assocArray.size(); i++){
list.add((TestA) Cast.cast(assocArray.get(i), TestA.class));
}
System.out.println(list.get(0).getId());
System.out.println(list.get(0).getName());
System.out.println(list.get(1).getId());
System.out.println(list.get(1).getName());
} catch (Exception e) {
e.printStackTrace();
}
public class TestA {
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
运行结果:
|