JAXBElement相关问题

时间:2011-08-11 15:00:19

标签: jaxb

我正在获取像

这样的JSON字符串数据

{ “用户名”: “KU”, “密码”: “KU”}。

如何将此字符串转换为JAXBElement对象。

请给我回答。

1 个答案:

答案 0 :(得分:0)

您可以使用Jackson轻松解组JSON。请参阅以下代码,了解上述用户名和密码。它将以下内容输出到控制台控制台,显示它已从JSON字符串创建了该类的实例。

{"username":"KU","password":"KU"} -> Username [KU], Password [KU]

import org.codehaus.jackson.map.ObjectMapper;

public class JaxbTest {
  public static void main(String[] args) throws Throwable {
    String json = "{\"username\":\"KU\",\"password\":\"KU\"}";
    ObjectMapper mapper = new ObjectMapper();
    JavaObject javaObject = mapper.readValue(json, JavaObject.class);

    System.out.println(json + " -> " + javaObject.toString());
  }

  private static class JavaObject {
    private String username;
    private String password;

    public JavaObject() { }

    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;
    }

    @Override
    public String toString() {
      return "Username [" + this.username + "], Password [" + this.password + "]";
    }
  }
}