如何将Json String映射到POJO?

时间:2014-09-16 05:50:32

标签: java json web-services rest jackson

我收到第三方网络服务的json回复。

    {
  "Values":[
    {
      "Date":"2013-08-01",
      "Value":1451674.0
    },
    {
      "Date":"2013-09-01",
      "Value":1535645.0
    },
    {
      "Date":"2013-10-01",
      "Value":1628753.0
    },
    {
      "Date":"2013-11-01",
      "Value":1279856.0
    },
    {
      "Date":"2013-12-01",
      "Value":1471991.0
    },
    {
      "Date":"2014-01-01",
      "Value":1571008.0
    },
    {
      "Date":"2014-02-01",
      "Value":1863232.0
    },
    {
      "Date":"2014-03-01",
      "Value":2126469.0
    },
    {
      "Date":"2014-04-01",
      "Value":2146069.0
    },
    {
      "Date":"2014-05-01",
      "Value":2735564.0
    },
    {
      "Date":"2014-06-01",
      "Value":1977808.0
    },
    {
      "Date":"2014-07-01",
      "Value":1932503.0
    }
  ]
}

现在pojo属性应该是什么以及如何用pojo映射它?

class Value{

    @JsonProperty("Values")
    List<DateValuePair> values;

//setter getter
}

class DateValuePair{
    @JsonProperty("Date")
    String date;

    @JsonProperty("Value")
    String value;
//setter getter
}

//映射

Value visits = new Value();
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
        mapper.configure(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true);

    HttpClient client = HttpClientBuilder.create().build();
    HttpGet request = new HttpGet("http://api.8df1fc");
    HttpResponse response = client.execute(request);
    BufferedReader rd = new BufferedReader (new InputStreamReader(response.getEntity().getContent()));
    String line = "";
    while ((line = rd.readLine()) != null) {
      System.out.println(line);
      mapper.readValue(line, Value.class);
    }

得到以下异常:

{"Values":[{"Date":"2013-08-01","Value":1451674.0},{"Date":"2013-09-01","Value":1535645.0},{"Date":"2013-10-01","Value":1628753.0},{"Date":"2013-11-01","Value":1279856.0},{"Date":"2013-12-01","Value":1471991.0},{"Date":"2014-01-01","Value":1571008.0},{"Date":"2014-02-01","Value":1863232.0},{"Date":"2014-03-01","Value":2126469.0},{"Date":"2014-04-01","Value":2146069.0},{"Date":"2014-05-01","Value":2735564.0},{"Date":"2014-06-01","Value":1977808.0},{"Date":"2014-07-01","Value":1932503.0}]}
Exception in thread "main" org.codehaus.jackson.map.JsonMappingException: Root name 'Values' does not match expected ('Value') for type [simple type, class com.domain.Value]
 at [Source: java.io.StringReader@e9a398d; line: 1, column: 2]

谢谢!

2 个答案:

答案 0 :(得分:1)

你可以拥有看起来像这样的Pojo

class Value{

    @JsonProperty("Values")
    List<DateValuePair> values;
}

class DateValuePair{
    @JsonProperty("Date")
    String date;

    @JsonProperty("Value")
    String value;
}

和getters / setteres

我们有jackson库,可以帮助您将json字符串反序列化到上面的POJO对象 只需一个你需要前进的指针

希望这有帮助!

答案 1 :(得分:0)

Root name 'Values' does not match expected ('Value') for type [simple type, class
com.domain.Value] at [Source: java.io.StringReader@e9a398d; line: 1, column: 2]

尝试将UNWRAP_ROOT_VALUE设为false

//mapper.configure(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, true);
mapper.configure(DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, false);