如何将Json字符串映射到POJO?

时间:2022-08-01 17:02:42

I am getting below json response from third party web services.

我正在从第三方web服务获得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
    }
  ]
}

Now what should be the pojo properties and how to map it with pojo?

那么pojo的属性应该是什么呢?如何用pojo来映射呢?

class Value{

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

//setter getter
}

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

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

//mapping

/ /映射

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

got below exception:

有以下例外:

{"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]

Thanks!

谢谢!

2 个解决方案

#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]

Try with setting UNWRAP_ROOT_VALUE to false

尝试将UNWRAP_ROOT_VALUE设置为false

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

#2


1  

you can have Pojo which looks something like this

你可以有Pojo看起来像这样

class Value{

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

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

    @JsonProperty("Value")
    String value;
}

and getters/setteres as well

和getter / setter

we have jackson libraries which will help you to de-serialize the json string to above POJO object Just a pointer you need to take it forward

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

Hope this helps!

希望这可以帮助!

#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]

Try with setting UNWRAP_ROOT_VALUE to false

尝试将UNWRAP_ROOT_VALUE设置为false

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

#2


1  

you can have Pojo which looks something like this

你可以有Pojo看起来像这样

class Value{

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

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

    @JsonProperty("Value")
    String value;
}

and getters/setteres as well

和getter / setter

we have jackson libraries which will help you to de-serialize the json string to above POJO object Just a pointer you need to take it forward

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

Hope this helps!

希望这可以帮助!