我正在围绕HTTPClient工作以发送POST请求并获得响应。我得到的响应代码没问题。
我想阅读该回复中的一些内容HttpEntity entity =response.getEntity();
我正在控制台System.out.println(EntityUtils.toString(entity));
上打印该响应。
正在打印{"created_at":"2011-12-01T07:56:50+00:00","type":"sample","id":29,"status":"received"}
我想从上面的字符串中读取id
。
任何人都可以帮助如何从实体中检索id。
提前致谢..
答案 0 :(得分:3)
数据为JSON数据。
您可以转到json.org并下载JSONObject。
你可以这样做,
JSONObject json = new JSONObject(EntityUtils.toString(entity));
int id = json.getInt("id");
答案 1 :(得分:0)
您的回复似乎是JSON之一。你可以使用JSON库,例如Jackson吗?
答案 2 :(得分:0)
这种方式不是很有效但你也可以试试。
BufferedReader br = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
String readLine;
while(((readLine = br.readLine()) != null)) {
System.err.println(readLine);
}
if(readLine.indexOf(",") > -1 && readLine.indexOf(":") > -1)
String id = (readLine.split(",")[2]).split(":")[1];