有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何忽略json字段,并使用Jackson ObjectMapper获取其值以进行映射

鉴于以下情况:

  {
    "countries":{
        "country":[{
            "name":"USA",
             "independence":"July 4, 1776",
         }],
     },
  }

我想忽略“国家”而获得国家的价值。因此,映射完json后,应该是这样的

{
  "countries": [{
            "name":"USA",
             "independence":"July 4, 1776",
         }],
 }

ObjectMapper目前是否可以实现这一点

编辑:以下是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;
}

我正在这么做

    return new ObjectMapper().readValue({jsonValue}, Result.class);

共 (1) 个答案

  1. # 1 楼答案

    输入json的语法错误。应该是这样的

      {
        "countries":{
            "country":[{
                "name":"USA",
                 "independence":"July 4, 1776"
             }]
         }
      }
    

    ObjectMapper用于将json转换为对象,并将对象转换为json。无论如何,要将第一个json转换为您想要的另一个json,您可以执行以下操作:

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

    这个类获取输入json,并通过将其打印到控制台将其转换为其他格式。此代码的输出为:

    {
      "countries" : [ {
        "name" : "USA",
        "independence" : "July 4, 1776"
      } ]
    }