使用 Jackson 检查 JSON 在 JAVA 中是否有效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29414366/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:13:45  来源:igfitidea点击:

Check if JSON is valid in JAVA using Hymanson

javajsonHymanson

提问by Abhishek

I have a JSON string which I am storing it in DB as a string. In front-end, I am rendering this JSON as object.

我有一个 JSON 字符串,我将它作为字符串存储在数据库中。在前端,我将此 JSON 渲染为对象。

I am using:

我在用:

JSON.parse(string);  
Uncaught Syntax error: Unexpected Token  

String :

细绳 :

{
"id": "295cd59f-4033-438c-9bf4-c571829f134e",
"from": "Shrisha S.<[email protected]>",
"to": [
    "Katie Porter <[email protected]>"
],
"cc": [
    "Hyman d<[email protected]>,     Keerthi<[email protected]>"
],
"bcc": [

 ]  
}

Is there any way I can check If JSON is valid or not in JAVA?

有什么方法可以检查 JSON 在 JAVA 中是否有效?

One thing to be noted here is that, I don't have a schema defined for JSON which I can map to, i.e. JSON can hold anything.

这里要注意的一件事是,我没有为 JSON 定义的模式可以映射到,即 JSON 可以容纳任何东西。

I am currently trying out with HymanSONbut for that I need a pre-defined schema which I don't have. Is there anyway this can be fixed?

我目前正在尝试,HymanSON但为此我需要一个我没有的预定义模式。无论如何这可以解决吗?

回答by pdem

You can read it as a JsonNode, no need to map it to a specific Class, its generic:

您可以将其作为 JsonNode 读取,无需将其映射到特定的类,即它的通用类:

try{

  ObjectMapper objectMapper = ...;
  JsonNode jsonNode = objectMapper.readTree(yourJsonString);

} catch(JsonProcessingException e){........}

回答by StaxMan

There are two different parts to the question. First is whether it is valid JSON, and second whether it contains specific set of information. @pdem already answered first part (you can also read JSON as java.lang.Objectto get the same effect).

这个问题有两个不同的部分。首先是它是否是有效的 JSON,其次是它是否包含特定的信息集。@pdem 已经回答了第一部分(您也可以阅读 JSONjava.lang.Object以获得相同的效果)。

But for second part, JSON Schema is not usually a good way, as it focuses on JSON aspects but not on more meaningful part of actual data, possible sub-typing and so on, which matter at Java level where all actual data processing occurs.

但对于第二部分,JSON Schema 通常不是一个好方法,因为它侧重于 JSON 方面,而不是实际数据中更有意义的部分、可能的子类型等,这些在 Java 级别上发生所有实际数据处理都很重要。

So usually you would define a POJO (or ideally just use one you use for actual data processing), bind to it (with ObjectMapper.readValue()), and then check whether data is not only technically valid wrt low-level data types, but also that it conforms to additional business constraints.

因此,通常您会定义一个 POJO(或者理想情况下只使用一个用于实际数据处理的 POJO),绑定到它(使用ObjectMapper.readValue()),然后检查数据是否不仅在技术上对低级数据类型有效,而且是否符合到额外的业务限制。

For latter part you can either write Java code, or use an annotation based framework such as Bean Validation API (JSR-303); see for example:

对于后面的部分,您可以编写 Java 代码,也可以使用基于注解的框架,例如 Bean Validation API (JSR-303);见例如:

http://beanvalidation.org/

http://beanvalidation.org/

plus there are many #bean-validation tagged questions here as well related to usage. Some frameworks add explicit support for it; for example the best Java service framework, DropWizard does this. Others like Spring Boot have support as well.

此外,这里还有许多 #bean-validation 标记的问题以及与使用相关的问题。一些框架为它添加了明确的支持;例如最好的 Java 服务框架,DropWizard 就是这样做的。其他如 Spring Boot 也有支持。

回答by Rohit Agre

JSON specification forbids it from using newline characters, make sure you are replacing newline characters see Regex replace all newline characters with comma

JSON 规范禁止使用换行符,请确保您正在替换换行符,请参阅 Regex 用逗号替换所有换行符

make sure you do this before storing it in DB.

确保在将其存储在 DB 之前执行此操作。