我正在使用以下代码将 InputStream 转换为 JSONObject。我的问题是,有什么简单的方法可以将 InputStream 转换为 JSONObject。不做 InputStream -> BufferedReader -> StringBuilder -> 循环 -> JSONObject.toString()。
InputStream inputStreamObject = PositionKeeperRequestTest.class.getResourceAsStream(jsonFileName);
BufferedReader streamReader = new BufferedReader(new InputStreamReader(inputStreamObject, "UTF-8"));
StringBuilder responseStrBuilder = new StringBuilder();
String inputStr;
while ((inputStr = streamReader.readLine()) != null)
responseStrBuilder.append(inputStr);
JSONObject jsonObject = new JSONObject(responseStrBuilder.toString());
最佳答案
由于您已经在使用 Google 的 Json-Simple
库,您可以像这样从 InputStream
解析 json:
InputStream inputStream = ... //Read from a file, or a HttpRequest, or whatever.
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject)jsonParser.parse(
new InputStreamReader(inputStream, "UTF-8"));
https://stackoverflow.com/questions/22461663/
相关文章:
json - 新的@angular/cli 版本中的文件 angular-cli.json 在哪里?
json - JSONObject 的 Jackson 2 等价物是什么?
json - 如何在 Angular 2 中获取 JSON 文件
ios - 使用 JSONEncoder 将 nil 值编码为 null
json - 在 Play Framework JsObject 中解析 Json 数组
json - 我应该如何处理 JSON 中的 HATEOAS 链接和引用?
java - 在 JAX-RS 2.0 客户端库中处理自定义错误响应