请在使用Java验证JSON Schema之前建议如何执行类型转换。我使用 json-schema-validation-pipeline 包在NodeJS中实现了相同的功能。下面的代码片段供参考(其中param1实际上是后端API提供的字符串类型)。
var ValidationPipeline = require('json-schema-validation-pipeline');
var V = ValidationPipeline.V;
var validate = ValidationPipeline([
{
$schema: {
'param1': V(Number).min(60)
}
},
{ $cast: { param1: Number } }
]);
所以基本上,我正在寻找Java中用于上述代码片段的等效解决方案。感谢
答案 0 :(得分:0)
将它分配给JAVA的POJO模型类,一旦你拥有了这个本机对象,那么就可以在语言操作中对任何东西进行类型转换例如 -
File file = new File("json/student.json");
// get json as buffer
BufferedReader br = new BufferedReader(new FileReader(file));
// obtained Gson object
Gson gson = new Gson(); //import com.google.gson.Gson;
// called fromJson() method and passed incoming buffer from json file
// passed student class reference to convert converted result as Student object
Student student = gson.fromJson(br, Student.class);