我正在尝试进行HTTP GET请求,该请求返回JSON响应。 我需要将JSON响应中的某些值存储在我的会话中。 我有以下代码:
public String getSessionKey(){
BufferedReader rd = null;
StringBuilder sb = null;
String line = null;
try {
URL url = new URL(//url here);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
sb = new StringBuilder();
while ((line = rd.readLine()) != null)
{
sb.append(line + '\n');
}
return sb.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
这会返回一个字符串形式的 JSON:
{ "StatusCode": 0, "StatusInfo": "Processed and Logged OK", "CustomerName": "Mr API"}
我需要将StatusCode和CustomerName存储在会话中。我该如何使用Java返回JSON?
谢谢
JsonNode
或 POJO)和许多其他事情。真的没有明确定义的角色 :) - fge