使用Jackson检查JSON在JAVA中是否有效 [英] Check if JSON is valid in JAVA using Jackson

查看:2337
本文介绍了使用Jackson检查JSON在JAVA中是否有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.parse(string);  
Uncaught Syntax error: Unexpected Token  

字符串:

{
"id": "295cd59f-4033-438c-9bf4-c571829f134e",
"from": "Shrisha S.<shrisha@s.com>",
"to": [
    "Katie Porter <katie.porter@ss.com>"
],
"cc": [
    "Jack d<jack.d@dd.com>,     Keerthi<keerthi.s@dd.com>"
],
"bcc": [

 ]  
}

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

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

这里需要注意的一点是,我没有为JSON定义的架构,我可以映射到它,即JSON可以容纳任何东西。

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.

我目前正在尝试使用 JACKSON 但为此我需要一个我没有的预定义架构。无论如何这可以修复吗?

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

推荐答案

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

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

try{

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

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

这篇关于使用Jackson检查JSON在JAVA中是否有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆