I'm using the Apache http classes to call a web service that returns a JSON object in the response body. I have a Jackson annotated java class mapped to the JSON object. I want to do something this, but google hasn't turned up the correct boilerplate.

    String url = hostName + uri;
    HttpGet httpGet = new HttpGet(url);
    HttpResponse response = httpclient.execute(httpGet);
    MyObject myObject = (MyObject)response.getEntity().getContent();

推荐答案

You have to use the ObjectMapper:

MyObject myObject = objectMapper.readValue(response.getEntity().getContent(), MyObject.class);

(An object mapper instance can be reused, so no need to create a new one for each deserialization)

Json相关问答推荐

Elasticsearch Go客户端错误:无效的SON格式:在中间件中索引文档时出错

盒子图显示不正确

NIFI-我需要数组的信息,但只需要第一个信息

使用json_query更新事实

Postgres Select json数组并重新映射属性名称

Vega-Lite规范:尽管在规范中提供了数据,但显示空图表

jq可以在两个JSON对象列表中依次添加对象吗?

如何加入或合并列表元素列表(未知长度)

从字节解码 JSON 数据,将 float 值更改为 int

使用 jq 获取特定键的所有父键

使用 Jolt 使用键中的值创建带有硬编码键和值的 JSON 数组

如何使用 gson 将数据保存在 json 文件中?

如何在golang中获取 struct 的json字段名称?

验证和格式化 JSON 文件

Jackson Scala 模块的小例子?

如何判断 JSON 响应元素是否为数组?

JSON Schema:验证数字或空值

JSON - 是否有任何 XML CDATA 类似功能?

如何从 jQuery ajax 调用将复杂对象传递给 ASP.NET WebApi GET?

杰克逊自动将 Joda DateTime 格式化为 ISO 8601 格式