随笔 - 441  文章 - 4  评论 - 84  阅读 - 109万 

有时候JSON 数据格式需要校验是否合法,我们可以使用 JsonSchema 来校验数据是否合法。

引入 pom.xml

https://json-schema.org/

<dependency>
            <groupId>com.networknt</groupId>
            <artifactId>json-schema-validator</artifactId>
            <version>1.4.0</version>
        </dependency>
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.networknt.schema.JsonSchema;
import com.networknt.schema.JsonSchemaFactory;
import com.networknt.schema.SpecVersion;
import com.networknt.schema.ValidationMessage;

import java.util.Set;

public class ValidJson {

    public static void main(String[] args) throws JsonProcessingException {
        String json="{\n" +
                " \"$schema\": \"http://json-schema.org/draft-07/schema#\",\n" +
                "    \"title\": \"Order Event\",\n" +
                "    \"description\": \"Order event schema for example\",\n" +
                "    \"required\": [\"order_id\", \"total_price\", \"products\" ],\n" +
                "    \"properties\": {\n" +
                "       \"order_id\": {\n" +
                "          \"type\": \"string\"\n" +
                "        },\n" +
                "        \"event\": {\n" +
                "          \"enum\": [\"PLACED\", \"DELIVERED\", \"RETURNED\"],\n" +
                "          \"type\": \"string\"\n" +
                "        },\n" +
                "        \"total_price\": { \n" +
                "         \"type\": \"number\",\n" +
                "             \"minimum\": 0\n" +
                "     },\n" +
                "        \"products\": {\n" +
                "      \"type\": \"array\",\n" +
                "      \"items\": {\n" +
                "        \"additionalProperties\": true,\n" +
                "        \"required\": [\"product_id\", \"price\"],\n" +
                "        \"minItems\": 1,\n" +
                "        \"properties\": {\n" +
                "          \"product_id\": {\n" +
                "            \"type\": \"string\"\n" +
                "          },\n" +
                "          \"price\": {\n" +
                "            \"type\": \"number\",\n" +
                "            \"minimum\": 0\n" +
                "          },\n" +
                "          \"quantity\": {\n" +
                "            \"type\": \"integer\"\n" +
                "          }\n" +
                "        }\n" +
                "      }\n" +
                "    }\n" +
                "   }\n" +
                "}";

        String json2="{\n" +
                "  \"order_id\":\"order134\",\n" +
                "   \"event\": \"PLACED\",\n" +
                "   \"products\": [\n" +
                "     {\n" +
                "       \"product_id\": \"product_1\",\n" +
                "        \"price\":20.5,\n" +
                "       \"quantity\":2\n" +
                "     }\n" +
                "   ],\n" +
                "   \"total_price\": 41\n" +
                "}";

        JsonSchema jsonSchema = jsonSchema(json);

        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonNode = objectMapper.readTree(json2);



        String str= validateJson(jsonSchema,jsonNode);

        System.err.println(str);

    }

    public static JsonSchema jsonSchema(String schema) {
        return JsonSchemaFactory
                .getInstance( SpecVersion.VersionFlag.V7 )
                .getSchema(schema);
    }

    public static String validateJson(JsonSchema jsonSchema, JsonNode jsonNode){
        Set<ValidationMessage> errors = jsonSchema.validate(jsonNode);
        return errors.toString();
    }
}

posted on   自由港  阅读(83)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· spring官宣接入deepseek,真的太香了~
历史上的今天:
2008-08-22 winform 编程代码片段
2007-08-22 .net2.0线程使用参数
点击右上角即可分享
微信分享提示