2017-02-14 55 views
-1

鉴于以下JSON如何忽略一个JSON场,并得到它的值用杰克逊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); 
+0

你的代码是什么样的? –

+0

@CássioMazzochiMolinreturn mapper.readValue({jsonValue},{pojo.class}); – user3137376

+0

你的POJO课程是什么样的? –

回答

0

输入JSON有一个错误的语法。它应该是这样的

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

ObjectMapper用于将json转换成Object和Object为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" 
    } ] 
}