【Java】基于JSON-Schema生成随机JSON的解决方案

该博客介绍了一个使用Java实现的解决方案,根据json-schema动态生成随机json数据。博主在搜索现有方案不满意后,决定自行开发,利用maven管理和hutool工具包,通过递归解析json-schema的结构,生成符合约束的随机字符串、整数等。最终实现了从json-schema到随机json串的转换,适用于复杂结构的json生成。
摘要由CSDN通过智能技术生成

一、需求

        1、给出特定格式json-schema,生成随机json串

        2、json串,目录结构按json-schema定义

        3、使用java开发语言

        4、不需要提供页面,能输出随机json串即可(控制台打印)

        5、json-schema中的结构可能存在多级,属性不确定,但结构统一

二、示例

1、json-schema

{
	"definitions": {},
	"$schema": "http://json-schema.org/draft-07/schema#", 
	"$id": "https://example.com/object1646817236.json", 
	"title": "Root", 
	"type": "object",
	"required": [
		"city",
		"number",
		"gov",
		"user"
	],
	"properties": {
		"city": {
			"$id": "#root/city", 
			"title": "City", 
			"type": "string",
			"default": "",
			"minLength": 1,
            "maxLength": 40
		},
		"number": {
			"$id": "#root/number", 
			"title": "Number", 
			"type": "integer",
			"minLength": 1,
            "maxLength": 6,
			"default": 0
		},
		"gov": {
			"$id": "#root/gov", 
			"title": "Gov", 
			"type": "object",
			"required": [
				"adrress",
				"personnum"
			],
			"properties": {
				"adrress": {
					"$id": "#root/gov/adrress", 
					"title": "Adrress", 
					"type": "string",
					"default": "",
			        "minLength": 1,
                    "maxLength": 40
				},
				"personnum": {
					"$id": "#root/gov/personnum", 
					"title": "Personnum", 
					"type": "integer",
			        "minLength": 1,
                    "maxLength": 8,
					"default": 0
				}
			}
		},
		"user": {
			"$id": "#root/user", 
			"title": "User", 
			"type": "array",
			"default": [],
			"items":{
				"$id": "#root/user/items", 
				"title": "Items", 
				"type": "object",
				"required": [
					"name",
					"age"
				],
				"properties": {
					"name": {
						"$id": "#root/user/items/name", 
						"title": "Name", 
						"type": "string",
						"default": "",
			            "minLength": 1,
                        "maxLength": 40
					},
					"age": {
						"$id": "#root/user/items/age", 
						"title": "Age", 
						"type": "integer",
			            "minLength": 1,
                        "maxLength": 4,
						"default": 0
					}
				}
			}

		}
	}
}

2、预期结果

{
	"city":"chicago",
	"number":20,
	"gov":{
		"adrress":"chicago",
		"personnum":10
	},
	"user":[
		{
			"name":"Alex",
			"age":20
		}
	]
}

三、解决方案

1、方案说明

1)度娘搜索的解决方案都不满足,大多数是固定schema,即字段固定

2)csdn上有将schema转java实体bean,也不能拿来用

3)最终决定自行实现

4)考虑快速实现问题,使用maven管理,使用hutool解决常规判断

2、方案依赖

1)jdk8

2)idea

3)maven项目

4)阿里maven库

5)hutool工具依赖

 3、方案思路

1)分析结构

        属性定义: properties

        属性类型: type

        取值范围: minLength与maxLength

2)抽取类型,有:object,string,integer,number,array

3)处理分析

        object: 拥有子节点

        string:生成随机字符串

        integer:生成随机数

        number: 与integer一致

        array:数组,需要单独处理,解析后类型参照string,integer

4)使用递归处理子集结构

 4、核心代码处理(示例图)

 5、结果示例图

 6、源码不免费哦

csdn资源-java解析json-schema,生成随机json

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值