我有一个班级 User
,我从system1收到JSON(用户类),我应该阅读信息,验证然后转发到system2,我无法触摸这些2系统,问题是键的名称不同,我想区分反序列化和序列化的名称
收到JSON是:
{"userId":"user1","pwd":"123456","country":"US"}
"{"username":"user1","password":"123456","country":"US"}"
但发送的应该是这样的
我正在使用Gson lib,这是我的代码,请帮助
用户类:
public class User implements Cloneable {
@SerializedName("username")
private String username ;
@SerializedName("password")
private String password ;
@SerializedName("country")
private String country ;
public User(String username, String password, String country) {
super();
this.username = username;
this.password = password;
this.country = country;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
TestJson类:
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class TestJson {
private static GsonBuilder gsonBuilder;
private static Gson gson;
public static Object fromJson(String json, Class clz) {
gson = new Gson();
return gson.fromJson(json, clz);
}
public static String toJson(Object obj) {
gsonBuilder = new GsonBuilder();
gson = gsonBuilder.create();
String json = gson.toJson(obj);
return json;
}
public static void main(String[] args) {
String json2 = "{\"userId\":\"user1\",\"pwd\":\"123456\",\"country\":\"US\"}";
User user=(User) TestJson.fromJson(json2, User.class);
System.out.println(user.getPassword());
User u =new User("user1","123456","US");
String json1=TestJson.toJson(u);
System.out.println(json1);
}
}
答案 0 :(得分:3)
您可以为此目的创建自定义序列化器/反序列化器。
串行:
public class UserSerializer implements JsonSerializer<User> {
@Override public JsonElement serialize(User obj, Type type, JsonSerializationContext jsonSerializationContext) {
..........
}
}
解串器:
public class UserDeserializer implements JsonDeserializer<User> {
@Override public User deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
...........
}
}
并创建Gson实例:
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(User.class, new UserSerializer());
gsonBuilder.registerTypeAdapter(User.class, new UserDeserializer());
Gson gson = gsonBuilder.create();
编辑:这是一个自定义反序列化器的示例,可能符合您的需要。在这种情况下,我们不需要自定义序列化程序。
添加此UserDeserializer.java
:
public class UserDeserializer implements JsonDeserializer<User> {
@Override
public User deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject obj = json.getAsJsonObject();
User user = new User(obj.get("userId").getAsString(), obj.get("pwd").getAsString(), obj.get("country").getAsString());
return user;
}
}
将fromJson
实现替换为此(我使用泛型来避免在调用fromJson
时需要强制转换):
public static <T> T fromJson(String json, Class<T> clz) {
gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(User.class, new UserDeserializer());
gson = gsonBuilder.create();
return gson.fromJson(json, clz);
}
答案 1 :(得分:2)
如果有其他字段名称,请使用@ SerializedName的<base href="/o2b2b/" />
参数
alternate
答案 2 :(得分:0)
我能想到的唯一方法是为JsonObject提供自定义适配器或deser,然后将其映射到用户。
使用Genson,您可以创建两个Genson实例,一个用于反序列化,另一个用于serializaiton。可以使用重命名的属性like that配置反序列化中使用的那个。
// you can also precise that you want to rename only the properties from User class
Genson genson = new GensonBuilder()
.rename("username", "userId")
.rename("password", "pwd")
.create();
User user = genson.deserialize(json, User.class);