我正在尝试发出一个返回 json 响应的 http get 请求。我需要将 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?
谢谢