Jackson JSON / XML根元素不一样 [英] Jackson JSON / XML root elements are not the same

查看:958
本文介绍了Jackson JSON / XML根元素不一样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,请注意我的问题非常类似于:
Jackson xml和json根元素
但只是稍微不同,我只需要JSON的单个根元素。

First, note that my issue is very similar to: Jackson xml and json root element but differs only slightly where I only want a single root element for JSON.

这是我的UserList类:

Here is my UserList class:

@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(目前我用用户而不是用户使用当前的测试代码):

Here is my desired JSON (currently I get "user" instead of "users" with the current test code):

{
  "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(这是我用当前测试代码得到的) :

Here is my desired XML (which is what I get with the current test code):

<?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的根元素是用户而不是用户。
任何帮助都将不胜感激。

My issue is I would like the root element of my JSON to be "users" and not "user". Any help would be appreciated.

推荐答案

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

@JacksonXmlProperty is how you can rename a property in xml. Then let name for JSON be the field name that I changed to users below. You can also remove most of the annotations you experimented with. This will produce expected XML and 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<>();
}

这篇关于Jackson JSON / XML根元素不一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆