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 數據以適應您的具體需求。