gpt4 book ai didi

java - Jackson JSON/XML 根元素不相同

转载 作者:行者123 更新时间:2023-11-30 06:28:12 24 4
gpt4 key购买 nike

首先,请注意我的问题与以下内容非常相似: Jackson xml and json root element但略有不同,我只想要 JSON 的单个根元素。

这是我的 UserList 类:

@XmlRootElement(name = "users")
@JsonRootName(value = "users")
@JsonTypeName(value = "users")
public class UserList {

// Tried all of these:
// @JacksonXmlElementWrapper(localName = "user")
// @JacksonXmlProperty(localName = "user")
// @JsonUnwrapped
// @JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NONE /**.NAME */)
// @JsonProperty("users") // <-- Sets both XML and JSON to 'users'
@JacksonXmlElementWrapper(useWrapping = false) // <-- This gets rid of duplicate 'users' in XML
public List<User> user = new ArrayList<User>();

public UserList() {}

}

这是我的用户类:

@XmlRootElement(name = "user")
@JsonRootName(value = "user")
@JsonInclude(Include.NON_NULL)
@XmlAccessorType(XmlAccessType.FIELD)
public class User {

private int userId;
private String userName;
private String password;
private long passwordUpdated;
private long passwordExpire;
private String sessionKey;

public User () {}

getters and setters here ...
}

这是我想要的 JSON(目前我在当前测试代码中得到“user”而不是“users”):

{
"users": [{
"userId": 1,
"userName": "test1@user.com",
"passwordUpdated": 0,
"passwordExpire": 0,
"sessionKey": "key"
}, {
"userId": 2,
"userName": "test2@user.com",
"passwordUpdated": 0,
"passwordExpire": 0,
"sessionKey": "key"
}]
}

这是我想要的 XML(这是我使用当前测试代码得到的):

<?xml version="1.0" encoding="UTF-8"?>
<users>
<user>
<userId>1</userId>
<userName>test1@user.com</userName>
<passwordUpdated>0</passwordUpdated>
<passwordExpire>0</passwordExpire>
<sessionKey>key</sessionKey>
</user>
<user>
<userId>2</userId>
<userName>test2@user.com</userName>
<passwordUpdated>0</passwordUpdated>
<passwordExpire>0</passwordExpire>
<sessionKey>key</sessionKey>
</user>
</users>

这是我的测试代码:

{

User user1 = new User();
user1.setUserId(1);
user2.setUserName("test1@user.com");
user1.setPasswordExpire(0);
user1.setPasswordUpdated(0);
user1.setSessionKey("key");

User user2 = new User();
user2.setUserId(1);
user2.setUserName("test2@user.com");
user2.setPasswordExpire(0);
user2.setPasswordUpdated(0);
user2.setSessionKey("key");

UserList userList = new UserList();
userList.user.add(user1);
userList.user.add(user2);

String json = MapperUtils.modelToJson(userList);

String xml = MapperUtils.modelToXml(userList);

}

public class MapperUtils {

final static ObjectMapper jsonMapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
final static ObjectMapper xmlMapper = new XmlMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

public static String modelToJson(final Object object) throws IOException {
return jsonMapper.writeValueAsString(object);
}

public static String modelToXml(final Object object) throws IOException {
return xmlMapper.writer().writeValueAsString(object);
}

}

我的问题是我希望 JSON 的根元素是“users”而不是“user”。任何帮助将不胜感激。

最佳答案

@JacksonXmlProperty 是您在 xml 中重命名属性的方法。然后让 JSON 的 name 为我在下面更改为 users 的字段名称。您还可以删除您尝试过的大部分注释。这将生成预期的 XML 和 JSON:

@JsonInclude(JsonInclude.Include.NON_NULL)
@XmlAccessorType(XmlAccessType.FIELD)
class User {
private int userId;
private String userName;
private String password;
private long passwordUpdated;
private long passwordExpire;
private String sessionKey;
}

@JsonRootName(value = "users")
class UserList {
@JacksonXmlProperty(localName = "user")
@JacksonXmlElementWrapper(useWrapping = false)
List<User> users = new ArrayList<>();
}

关于java - Jackson JSON/XML 根元素不相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46676318/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com