Java JSON Schema 库,如 jsonschema-core
和 jsonschema-validator
,可以帮助您处理和验证复杂的数据结构。以下是一些关于如何使用这些库处理复杂数据的示例。
首先,确保将所需的依赖项添加到项目中。对于 Maven 项目,可以在 pom.xml
文件中添加以下依赖项:
<dependency>
<groupId>com.github.fge</groupId>
<artifactId>jsonschema-core</artifactId>
<version>1.13.0</version>
</dependency>
<dependency>
<groupId>com.github.fge</groupId>
<artifactId>jsonschema-validator</artifactId>
<version>2.2.6</version>
</dependency>
接下来,我们将处理一个包含嵌套对象和数组的复杂 JSON 数据。
{
"name": "John Doe",
"age": 30,
"address": {
"street": "123 Main St",
"city": "New York",
"state": "NY",
"zip": "10001"
},
"phoneNumbers": [
{
"type": "home",
"number": "555-555-1234"
},
{
"type": "work",
"number": "555-555-5678"
}
]
}
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "integer",
"minimum": 0
},
"address": {
"$ref": "#/definitions/address"
},
"phoneNumbers": {
"type": "array",
"items": {
"$ref": "#/definitions/phoneNumber"
}
}
},
"definitions": {
"address": {
"type": "object",
"properties": {
"street": {
"type": "string"
},
"city": {
"type": "string"
},
"state": {
"type": "string"
},
"zip": {
"type": "string",
"format": "uuid"
}
},
"required": ["street", "city", "state", "zip"]
},
"phoneNumber": {
"type": "object",
"properties": {
"type": {
"type": "string"
},
"number": {
"type": "string"
}
},
"required": ["type", "number"]
}
}
}
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.github.fge.jsonschema.core.exceptions.ProcessingException;
import com.github.fge.jsonschema.core.report.ProcessingReport;
import com.github.fge.jsonschema.main.JsonSchema;
import com.github.fge.jsonschema.main.JsonSchemaFactory;
import com.github.fge.jsonschema.main.JsonValidator;
public class JsonSchemaExample {
public static void main(String[] args) {
String json = "{\"name\":\"John Doe\",\"age\":30,\"address\":{\"street\":\"123 Main St\",\"city\":\"New York\",\"state\":\"NY\",\"zip\":\"10001\"},\"phoneNumbers\":[{\"type\":\"home\",\"number\":\"555-555-1234\"},{\"type\":\"work\",\"number\":\"555-555-5678\"}]}";
String schema = "{\"$schema\":\"http://json-schema.org/draft-07/schema#\",\"type\":\"object\",\"properties\":{\"name\":{\"type\":\"string\"},\"age\":{\"type\":\"integer\",\"minimum\":0},\"address\":{\"$ref\":\"#/definitions/address\"},\"phoneNumbers\":{\"type\":\"array\",\"items\":{\"$ref\":\"#/definitions/phoneNumber\"}},\"definitions\":{\"address\":{\"type\":\"object\",\"properties\":{\"street\":{\"type\":\"string\"},\"city\":{\"type\":\"string\"},\"state\":{\"type\":\"string\"},\"zip\":{\"type\":\"string\",\"format\":\"uuid\"}},\"required\":[\"street\",\"city\",\"state\",\"zip\"]},\"phoneNumber\":{\"type\":\"object\",\"properties\":{\"type\":{\"type\":\"string\"},\"number\":{\"type\":\"string\"}},\"required\":[\"type\",\"number\"]}}}";
try {
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode jsonNode = (ObjectNode) objectMapper.readTree(json);
JsonNode schemaNode = objectMapper.readTree(schema);
JsonSchemaFactory schemaFactory = JsonSchemaFactory.getInstance();
JsonSchema jsonSchema = schemaFactory.getJsonSchema(schemaNode);
JsonValidator validator = JsonValidator.getInstance();
ProcessingReport report = validator.validate(jsonSchema, jsonNode);
if (report.isSuccess()) {
System.out.println("JSON is valid.");
} else {
System.out.println("JSON is invalid: " + report.getErrors());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
在这个示例中,我们首先将 JSON 数据和 JSON Schema 数据解析为 Java 对象。然后,我们使用 JsonSchemaFactory
创建一个 JsonSchema
实例,并使用 JsonValidator
验证 JSON 数据是否符合 Schema 定义。最后,我们检查验证报告以确定 JSON 数据是否有效。
这个示例展示了如何使用 Java JSON Schema 库处理包含嵌套对象和数组的复杂数据。您可以根据需要修改 JSON 数据和 Schema 数据以适应您的具体需求。