Java 将 InputStream 解析为 Json 对象并获取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23057064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parsing InputStream into Json object and getting a value
提问by Shashi Ranjan
The response json:
响应json:
{
"filename": "vcops-6.0.0-MPforAWS-1.1-1695068.pak",
"links": [
{
"rel": "pak_information",
"href": "https://<IP>:443/casa/upgrade/cluster/pak/MPforAWS-600/information"
},
{
"rel": "pak_file_information",
"href": "https://<IP>:443/casa/upgrade/slice/pak/MPforAWS-600/file_information"
},
{
"rel": "pak_cluster_status",
"href": "https://<IP>:443/casa/upgrade/cluster/pak/MPforAWS-600/status"
}
],
"pak_id": "MPforAWS-600"
}
I am using one helper of the framework we have. Framework returns response as "InputStream".
I want to get "pak_id" from this "InputStream". I tried with inputStreamObj.toString()
this does not work for me.
我正在使用我们拥有的框架的一个助手。框架将响应返回为“InputStream”。我想从这个“InputStream”中得到“pak_id”。我试过inputStreamObj.toString()
这对我不起作用。
The method I am using is:
我使用的方法是:
private String getPakId(InputStream uploadResponse) {
String pakId = null;
try {
String responseString = readInputStream(uploadResponse);
JSONObject jObj = new JSONObject(responseString);
pakId = jObj.getString("pak_id").trim();
Reporter.log("Pak id is=" + pakId, true);
} catch (Exception e) {
Reporter.log("Error in getting pak_id " + e.getMessage(), true);
}
return pakId;
}
and
和
private String readInputStream(InputStream inputStream) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(
inputStream, "UTF-8"));
String tmp;
StringBuilder sb = new StringBuilder();
while ((tmp = reader.readLine()) != null) {
sb.append(tmp).append("\n");
}
if (sb.length() > 0 && sb.charAt(sb.length() - 1) == '\n') {
sb.setLength(sb.length() - 1);
}
reader.close();
return sb.toString();
}
回答by Javi Fernández
If you only want the pak_id
value using only the standard library:
如果您只想要pak_id
使用标准库的值:
InputStream is = ...
String line;
StringBuilder text = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
while((line = reader.readLine()) != null) {
text.append(line).append(" ");
}
String pakId = text.toString().replaceAll(".*\"pak_id\": \"([^\"]+)\".*", "");
回答by Arnout Engelen
If you look at the documentation for InputStream, you'll notice it will not promise you that toString
will present you the contents of the stream.
如果您查看InputStream的文档,您会注意到它不会向您保证toString
会向您展示流的内容。
If you're not interested in actually streaming the stream (which is reasonable if you expect the response to be small, like it appears to be the case here), it's OK to first get all the bytes from the stream, put them into a String
, and then parse the String
.
如果您对实际流式传输流不感兴趣(如果您希望响应很小,这是合理的,就像这里的情况一样),可以首先从流中获取所有字节,将它们放入String
,然后解析String
.
To get the String
out of the InputStream
, I'd recommend IOUtils.toString
from apache commons-io.
为了让String
出来的InputStream
,我建议IOUtils.toString
从Apache的百科全书-io的。
回答by ender2101
Well, better late than never... the following worked for me:
好吧,迟到总比不到好……以下对我有用:
JSONTokener tokener = new JSONTokener(new InputStreamReader(istream));
JSONObject result;
try {
result = new JSONObject(tokener);
} catch (JSONException e) {
// Handle me!
}