【问题标题】:How to map Json String to POJO?如何将 Json 字符串映射到 POJO?
【发布时间】:2014-09-16 05:50:31
【问题描述】:

我收到来自第三方网络服务的低于 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]

谢谢!

【问题讨论】:

  • 更多细节,你试过什么代码,使用什么库,什么是pojo类???
  • Wundwin 建议正确!请尝试
  • 我试过 Vihar .. 效果很好.. 非常感谢您的帮助。

标签: java json web-services rest jackson


【解决方案1】:

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

class Value{

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

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

    @JsonProperty("Value")
    String value;
}

还有getter/setteres

我们有杰克逊库,可以帮助您将 json 字符串反序列化为上述 POJO 对象 只是一个你需要向前推进的指针

希望这会有所帮助!

【讨论】:

  • 我遇到了异常:线程“main”中的异常 org.codehaus.jackson.map.JsonMappingException:根名称“值”与类型 [简单类型, [来源:java.io.StringReader@e9a398d;类 com.domain.Value];行:1,列:2]
  • 你能展示你写的代码吗!它会有所帮助
  • 当然..我粘贴了我的问题。
【解决方案2】:
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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-16
    • 2021-11-17
    • 2014-04-12
    • 2018-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多