查看文档和源代码,我没有看到明确的方法。好奇,如果我遗失了什么。
Looking through the documentation and source code I don't see a clear way to do this. Curious if I'm missing something.
说我从服务器响应中收到一个InputStream。我从这个InputStream创建一个JsonParser。预计服务器响应是包含有效JSON的文本,例如:
Say I receive an InputStream from a server response. I create a JsonParser from this InputStream. It is expected that the server response is text containing valid JSON, such as:
{iamValidJson:yay}
但是,如果响应最终是无效的JSON或根本不是JSON,例如:
However, if the response ends up being invalid JSON or not JSON at all such as:
一些不是JSON的文本
JsonParser最终会抛出异常。在这种情况下,我希望能够从JsonParser中提取底层无效文本一些不是JSON 的文本,以便它可以用于其他目的。
the JsonParser will eventually throw an exception. In this case, I would like to be able to extract the underlying invalid text "Some text that is not JSON" out of the JsonParser so it can be used for another purpose.
我无法将其拉出InputStream,因为它不支持重置,而JsonParser的创建会消耗它。
I cannot pull it out of the InputStream because it doesn't support resetting and the creation of the JsonParser consumes it.
有办法吗?
推荐答案如果你有 JsonParser 然后你可以使用 jsonParser.readValueAsTree()。toString()。
If you have the JsonParser then you can use jsonParser.readValueAsTree().toString().
然而,这个可能需要解析的JSON确实是有效的JSON。
However, this likely requires that the JSON being parsed is indeed valid JSON.
更多推荐
如何从JsonParser获取底层字符串(Jackson Json)
发布评论