I'm trying to serialze part of a JSON string into an object. The JSON string looks as follows:
{"error":null,"excludeFields":null,"message":null,"success":{"user":{"name":null,"organizationId":100,"username":"nl4321"}}}
I only need the user-part of the JSON string, which corresponds to an object of the following class:
public class UserForm implements Serializable {
private static final long serialVersionUID = -5033294929007794646L;
private String username;
private String name;
@Getter @Setter
private int organizationId;
public UserForm() {
}
public UserForm(User user) {
if (user != null) {
this.username = user.getUsername();
this.name = user.getName();
this.organizationId = user.getDefaultOrganisationId();
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
The way I'm currently trying to deserialize it, is as follows:
private UserForm deserializeToJsonResponse(String bodyContent) throws IOException {
JSONDeserializer<UserForm> jsonDeserializer = new JSONDeserializer<UserForm>();
return jsonDeserializer.use("values.success.user", UserForm.class).deserialize(bodyContent, UserForm.class);
}
The use
method is provided with both a path and a class, but no matter what path I try, the contents of the UserForm are null after deserialization. I've tried: "user", "success.user", "values.success.user".
Does anyone have an idea what I'm doing wrong and how I can fix it? I realize there are other solutions out there, like Jackson, but this is part of a large codebase that already uses FlexJSON a lot. Background: I'm trying to write unit tests for an API that's part of the project.
来源:https://stackoverflow.com/questions/13843270/how-can-i-deserialize-part-of-a-json-string-into-an-object-using-flexjson