Given the below json
{
"countries":{
"country":[{
"name":"USA",
"independence":"July 4, 1776",
}],
},
}
I will like to ignore "countries" and get the value of country instead. hence, after mapping my json should look as so
{
"countries": [{
"name":"USA",
"independence":"July 4, 1776",
}],
}
Is this currently possible with ObjectMapper?
EDIT: below are the Pojo
public class Result {
private static final long serialVersionUID = 6403654749443599206L;
@Getter @Setter @JsonProperty("country") private List<Country> countries;
}
public class Country {
private static final long serialVersionUID = 6403654749443599206L;
@Getter @Setter private String name;
@Getter @Setter private String independence;
}
and I am doing this
return new ObjectMapper().readValue({jsonValue}, Result.class);
The input json has a wrong syntax. It should be like this
{
"countries":{
"country":[{
"name":"USA",
"independence":"July 4, 1776"
}]
}
}
ObjectMapper is used to turn json into Object and Object into json. Anyway, to convert the first json to the other you desire you can do something like this:
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import java.io.IOException;
import java.util.List;
public class ConvertExample {
public static void main(String[]args) {
String inputJson = new String("{\"countries\":{\"country\":[{\"name\":\"USA\", \"independence\":\"July 4, 1776\"}]}}");
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT); //To indent output, optional
try {
StartPojo startPojo = mapper.readValue(inputJson, StartPojo.class);
EndPojo endPojo = new EndPojo();
endPojo.countries = startPojo.getCountries().getCountry();
String outputJson = mapper.writeValueAsString(endPojo);
System.out.println("Output:");
System.out.println(outputJson);
} catch (IOException e) {
e.printStackTrace(); //Cannot read the input json
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
public static class StartPojo {
private Countries countries;
public Countries getCountries() {
return countries;
}
public void setCountries(Countries countries) {
this.countries = countries;
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
public static class EndPojo {
private List<Country> countries;
public List<Country> getCountries() {
return countries;
}
public void setCountries(List<Country> countries) {
this.countries = countries;
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Countries {
private List<Country> country;
public List<Country> getCountry() {
return country;
}
public void setCountry(List<Country> country) {
this.country = country;
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Country {
private String name;
private String independence;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIndependence() {
return independence;
}
public void setIndependence(String independence) {
this.independence = independence;
}
}
}
This class takes the input json and convert it to the other format by printing it into the console. The ouput of this code is:
{
"countries" : [ {
"name" : "USA",
"independence" : "July 4, 1776"
} ]
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.