【发布时间】:2016-06-08 11:48:07
【问题描述】:
您好,我正在尝试使用基本身份验证从 URL 解析 json 数据。数据来自服务器。
HttpHost target = new HttpHost(targetUrl, 443, "https");
HttpHost proxy = new HttpHost(proxyHost, Integer.valueOf(proxyPort), "http");
RequestConfig config = RequestConfig.custom().setConnectTimeout(5000).setConnectionRequestTimeout(5000)
.setProxy(proxy).setAuthenticationEnabled(true).build();
HttpGet httpget = new HttpGet(targetURI + orderId + "/prod.json");
httpget.setConfig(config);
response = httpClient.execute(target, httpget);
System.out.println(" Response ::" + EntityUtils.toString(response.getEntity()));
它显示json数据,如
[
{
"id": 1,
"order_id": 100,
"product_id": 113,
"order_address_id": 1,
"name": "Paper 002",
}
]
我也有这个 Json 数据的 pojo 类。但是当我试图读取显示数据时,它会抛出错误。我使用 jackson 读取该数据读取数据的代码是。
String jsondata = EntityUtils.toString(response.getEntity());
ObjectMapper objMapper = new ObjectMapper();
ProDesc pd = objMapper.readValue(jsondata, ProDesc.class);
System.out.println("** ID " + pd.getId());
那么如何使用jackson解析数据呢?我已经使用上面的代码显示了 json 数据。
【问题讨论】:
-
您的 ProDesc 类的属性名称是否与 Json 中的属性名称相同?
-
是的,我的属性名称与 comming json @kedarkamthe 相同
-
你的
ProDesc类是什么样的? -
@Narcis 我的 pojo 类就像:public class MyPojo { private int id;和 getter setter}