如何从JsonParser获取底层字符串(Jackson Json)

编程入门 行业动态 更新时间:2024-10-26 16:35:15
本文介绍了如何从JsonParser获取底层字符串(Jackson Json)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

查看文档和源代码,我没有看到明确的方法。好奇,如果我遗失了什么。

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)

本文发布于:2023-10-12 00:03:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483221.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   底层   JsonParser   Jackson   Json

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!