带有嵌入对象的JSON到Java Bean

时间:2014-03-31 04:19:32

标签: java json jackson

我有以下JSON数据:

{
  "role": "DOCTOR",
  "sex": "male",
  "secret": "manju",
  "address": {
     "street": "HSR",
     "city": "Bangalore",
     "state": "Karnataka",
     "postcode": "560102",
     "country": "India",
     "phonenumbers": [9880517151],
     "email": "manju.reddys@gmail.com"
  },
  "firstname": "Manjunath",
  "lastname": "Reddy",
  "dateofbirth": "13\/11\/1981",
  "education": "BE",
  "highlights": null
}

并且是2个java类

public abstract class User {   

public User() {
}

public User(ObjectId id) {
    this.id = id;
}

public void setId(String id) {
    this.id = new ObjectId(id);
}

public ObjectId getId() {
    return id;
}

/**
 * @return the firstName
 */
public String getFirstName() {
    return firstName;
}

/**
 * @param firstName the firstName to set
 */
public void setFirstName(String firstName) {
    this.firstName = firstName;
}

/**
 * @return the lastName
 */
public String getLastName() {
    return lastName;
}

/**
 * @param lastName the lastName to set
 */
public void setLastName(String lastName) {
    this.lastName = lastName;
}

/**
 * @return the dateOfBirth
 */
public Date getDateOfBirth() {
    return dateOfBirth;
}

/**
 * @param dateOfBirth the dateOfBirth to set
 */
public void setDateOfBirth(Date dateOfBirth) {
    this.dateOfBirth = dateOfBirth;
}

/**
 * @return the registeredOn
 */
public Date getRegisteredOn() {
    return registeredOn;
}

/**
 * @param registeredOn the registeredOn to set
 */
protected void setRegisteredOn(Date registeredOn) {
    this.registeredOn = registeredOn;
}

/**
 * @return the sex
 */
public String getSex() {
    return sex;
}

/**
 * @param sex the sex to set
 */
public void setSex(String sex) {
    this.sex = sex;
}

/**
 * @return the active
 */
public boolean isActive() {
    return active;
}

/**
 * @param active the active to set
 */
public void setActive(boolean active) {
    this.active = active;
}

/**
 * @return the address
 */
public Address getAddress() {
    return address;
}

/**
 * @param address the address to set
 */
public void setAddress(Address address) {
    this.address = address;
}

/**
 * @return the type
 */
public Role getRole() {
    return role;
}

/**
 * @param role the type to set
 */
public void setRole(Role role) {
    this.role = role;
}

/**
 * @return the education
 */
public String getEducation() {
    return education;
}

/**
 * @param education the education to set
 */
protected void setEducation(String education) {
    this.education = education;
}

@Override
public String toString() {
    try {
        return new ObjectMapper().writeValueAsString(this);
    } catch (JsonProcessingException ex) {
        return ex.getOriginalMessage();
    }
}

/**
 * @return the fullName
 */
public String getFullName() {
    return fullName;
}

/**
 * @param fullName the fullName to set
 */
public void setFullName(String fullName) {
    this.fullName = fullName;
}

public void setHighlights(List<AbnormalHighlight> highlights) {
    this.highlights = highlights;
}

public List<AbnormalHighlight> getHighlights() {
    return highlights;
 }

public enum Role {
    DOCTOR,
    PATIENT,
    RECEPTIONIST,
    NURSE,
    AMIN,
    LABTECHNICIAN,
    PARAMETRICIAN
 }    

@Id
@JsonSerialize(using = ObjectIdJsonSerializer.class)
private ObjectId id;

private String firstName;
private String lastName;
private Date dateOfBirth;
private Date registeredOn;
private String sex;
private boolean active;
private String fullName;

@Embedded    
private Address address;

@Embedded

private List<AbnormalHighlight> highlights;

private Role role;

private String education;
}

地址类:

@Embedded
public class Address implements Serializable {

private String street;

private String city;

private String postCode;

private String state;

private String cuntry;

private List<Number> phoneNumbers;

private String emailId;

/**
 * @return the street
 */
public String getStreet() {
    return street;
}

/**
 * @param street the street to set
 */
public void setStreet(String street) {
    this.street = street;
}

/**
 * @return the postCode
 */
public String getPostCode() {
    return postCode;
}

/**
 * @param postCode the postCode to set
 */
public void setPostCode(String postCode) {
    this.postCode = postCode;
}

/**
 * @return the state
 */
public String getState() {
    return state;
}

/**
 * @param state the state to set
 */
public void setState(String state) {
    this.state = state;
}

/**
 * @return the cuntry
 */
public String getCuntry() {
    return cuntry;
}

/**
 * @param cuntry the cuntry to set
 */
public void setCuntry(String cuntry) {
    this.cuntry = cuntry;
}

/**
 * @return the phoneNumbers
 */
public List<Number> getPhoneNumbers() {
    return phoneNumbers;
}

/**
 * @param phoneNumbers the phoneNumbers to set
 */
public void setPhoneNumbers(List<Number> phoneNumbers) {
    this.phoneNumbers = phoneNumbers;
}

/**
 * @return the emailId
 */
public String getEmailId() {
    return emailId;
}

/**
 * @param emailId the emailId to set
 */
public void setEmailId(String emailId) {
    this.emailId = emailId;
}    

/**
 * @return the city
 */
public String getCity() {
    return city;
}

/**
 * @param city the city to set
 */
public void setCity(String city) {
    this.city = city;
}

}

我正在尝试将JSON转换为上面的Java bean但是它 "throwing com.fasterxml.jackson.databind.JsonMappingException: Unexpected token (START_OBJECT), expected START_ARRAY: need JSON Array to contain As.WRAPPER_ARRAY type information for class com.kushan.vertx.api.User"错误。

以下是我用来转换的代码:

   public static <T> T fromJson(String json, Class<T> clazz) {
        try {
           final ObjectMapper om = new ObjectMapper();
           om.enableDefaultTyping();
        System.out.println("JSON DAta \n" + json + " \n\n");
        return om.readValue(json, clazz);
    } catch (JsonProcessingException ex) {
        ex.printStackTrace();
        return null;
    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
}

可以帮助理解如何将JSON转换为java吗?

0 个答案:

没有答案