如何将提交的InputStream解析为JSONObject?

时间:2018-11-23 22:37:13

标签: java android json android-studio inputstream

我从{em> class1 到 class2 提交了InputStream in 。现在,我想将 class2 中的InputStream解析为JSONObject

try {
        JsonElement element = new JsonParser().parse(new InputStreamReader(in));
        JSONObject jsonObject = new JSONObject(element.getAsJsonObject().toString());
} catch (JSONException e) {
        System.err.println(e.getMessage());
}

我的JsonElement null 。所以我没有JSONObject

我在 class1 中使用了相同的代码,并且可以正常工作。唯一的区别是InputStream是在 class1 中与HTTPHandler.execute(...)生成的。

class1 提交与{em> class2 正确的InputStream

1 个答案:

答案 0 :(得分:0)

您可以使用JsonReader,它支持从Input Stream读取json,并且ref doc提供了详细的示例。