Я хочу конвертировать XML в JSON, используя библиотеку Джексона. Я попробовал приведенный ниже код, чтобы получить Json, как показано ниже, используя Джексона, но я не могу. если я попробую использовать json, я могу, но я хочу знать, как это сделать с библиотекой Джексона.
@RequestMapping(value="/convertXMLtoJson",method=RequestMethod.POST,consumes = MediaType.APPLICATION_XML_VALUE)
public Map<String,Object> convertXMLtoJson(@RequestBody String strXMLData) {
Map<String,Object> objresponseMessage = null;
ObjectMapper objObjectMapper = new ObjectMapper();
Employee objEmployee = null;
try {
JSONObject obj = XML.toJSONObject(strXMLData);
ObjectMapper objectMapper = new XmlMapper();
objEmployee = objectMapper.readValue(strXMLData, Employee.class);
objEmployeeService.save(objEmployee);
} catch (Exception e) {
e.printStackTrace();
}
return objresponseMessage;
}
com.fasterxml.jackson.databind.exc.MismatchedInputException: Невозможно десериализовать экземпляр java.util.HashSet
из токена VALUE_STRING
в [Источник: (StringReader); строка: 5, столбец: 15] (через цепочку ссылок: com.example.springboot.bean.Employee ["Skills"])
<EmployeeDetail>
<FirstName>A</FirstName>
<LastName>Z</LastName>
<Age>20</Age>
<Skills>Java</Skills>
<Skills>J2EE</Skills>
<Skills>MSSQl</Skills>
<Skills>JAVA</Skills>
<TotalExperience>4</TotalExperience>
<JobDetails>
<CompanyName>1</CompanyName>
<Experience>1.5</Experience>
<Technologies>Java</Technologies>
<Technologies>WebServices</Technologies>
<Technologies>MSSQL</Technologies>
<Technologies>J2EE</Technologies>
<Location>India</Location>
</JobDetails>
<JobDetails>
<CompanyName>2</CompanyName>
<Experience>2.5</Experience>
<Technologies>Java</Technologies>
<Technologies>J2EE</Technologies>
<Technologies>MySQL</Technologies>
<Technologies>Spring</Technologies>
<Location>India</Location>
</JobDetails>
</EmployeeDetail>
Ожидаемый Json как (Выход исключен)
{
"EmployeeDetail": {
"FirstName": "A",
"LastName": "Z",
"Age": "20",
"Skills": [
"Java",
"J2EE",
"MSSQl",
"JAVA"
],
"TotalExperience": "4",
"JobDetails": [
{
"CompanyName": "1",
"Experience": "1.5",
"Technologies": [
"Java",
"WebServices",
"MSSQL",
"J2EE"
],
"Location": "India"
},
{
"CompanyName": "2",
"Experience": "2.5",
"Technologies": [
"Java",
"J2EE",
"MySQL",
"Spring"
],
"Location": "India"
}
]
}
}
Может ли кто-нибудь помочь мне понять, используя библиотеку Джексона
import java.io.Serializable;
import java.util.List;
import java.util.Set;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
@Document
@JacksonXmlRootElement(localName="EmployeeDetail")
public class Employee implements Serializable {
@Id
private String userId;
@JacksonXmlProperty(localName="FirstName")
@JsonProperty("FirstName")
private String firstName;
@JacksonXmlProperty(localName="LastName")
@JsonProperty("LastName")
private String lastName;
@JacksonXmlProperty(localName="Age")
@JsonProperty("Age")
private Integer age;
@JacksonXmlElementWrapper(localName="Skills")
@JsonProperty("Skills")
private Set<String> skills;
@JacksonXmlProperty(localName="JobDetails")
@JsonProperty("JobDetails")
private List<JobDetails> jobDetails;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public List<JobDetails> getJobDetails() {
return jobDetails;
}
public void setJobDetails(List<JobDetails> jobDetails) {
this.jobDetails = jobDetails;
}
public Set<String> getSkills() {
return skills;
}
public void setSkills(Set<String> skills) {
this.skills = skills;
}
}
import java.util.Set;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
@JacksonXmlRootElement(localName="JobDetails")
public class JobDetails {
JobDetails(String str) {
}
@JacksonXmlProperty(localName="CompanyName")
@JsonProperty("CompanyName")
private String companyName;
@JacksonXmlProperty(localName="Experience")
@JsonProperty("Experience")
private Integer experience;
@JacksonXmlProperty(localName="Location")
@JsonProperty("Location")
private String location;
@JacksonXmlElementWrapper(localName="Technologies")
@JsonProperty("Technologies")
private Set<String> technologies;
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public Integer getExperience() {
return experience;
}
public void setExperience(Integer experience) {
this.experience = experience;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public Set<String> getTechnologies() {
return technologies;
}
public void setTechnologies(Set<String> technologies) {
this.technologies = technologies;
}
}