在java中从POST读取json文件
我怎样才能像这样解压json文件在java中从POST读取json文件,java,json,Java,Json,我怎样才能像这样解压json文件 { "packageId": "11", "jsScript": "var divideFn = function(a,b) { return a/b} ", "functionName": "divideFn", "tests": [ { "testName": "test1", "expectedResult": "2.0", "param
{
"packageId": "11",
"jsScript": "var divideFn = function(a,b) { return a/b} ",
"functionName": "divideFn",
"tests": [
{
"testName": "test1",
"expectedResult": "2.0",
"params": [
2,
1
]
}
]
}
我有一个类可以很好地处理packageId、jsScrript、functionName,但不能处理测试
public class Data {
private final int packageId;
private final String jsScript;
private final String functionName;
private final List<Tests> tests;
@JsonCreator
public Data(@JsonProperty("packageId") String packageId,
@JsonProperty("jsScript") String jsScript,
@JsonProperty("functionName") String functionName,
@JsonProperty("tests") List<Tests> tests) {
this.packageId = Integer.parseInt(packageId);
this.jsScript= jsScript;
this.functionName = functionName;
this.tests = tests;
}
}
public class Tests{
public final String testName;
public final int expectedResult;
@JsonCreator
public Tests(@JsonProperty("testName") String testName,
@JsonProperty("expectedResult") String expectedResult){
this.testName= testName;
this.expectedResult = Integer.parseInt(expectedResult);
}
}
公共类数据{
私有最终int包ID;
私有最终字符串jsScript;
私有最终字符串functionName;
私人最终名单测试;
@JsonCreator
公共数据(@JsonProperty(“packageId”)字符串packageId,
@JsonProperty(“jsScript”)字符串jsScript,
@JsonProperty(“functionName”)字符串functionName,
@JsonProperty(“测试”)列表测试){
this.packageId=Integer.parseInt(packageId);
this.jsScript=jsScript;
this.functionName=functionName;
这个。测试=测试;
}
}
公开课考试{
公共最终字符串testName;
公开最终预期结果;
@JsonCreator
公共测试(@JsonProperty(“testName”)字符串testName,
@JsonProperty(“expectedResult”)字符串(expectedResult){
this.testName=testName;
this.expectedResult=Integer.parseInt(expectedResult);
}
}
我应该在课堂上做些什么改变才能让它很好地发挥作用?
我还尝试读取字符串之类的测试,但没有帮助似乎您在将JSON数组
测试反序列化为对象时遇到了一些问题。有几种方法可以解决这个问题
方法1
在类测试中添加@JsonIgnoreProperties(ignoreUnknown=true)
,以防止代码出现无法识别的字段“params”的异常
方法2
如果JSON数组不重要,并且您以后不会进一步解析它,则将其反序列化为测试
到列表
方法3
使用以下类将JSON数组测试
映射到列表
类测试{
私有字符串testName;
私人浮动预期结果;
私有列表参数;
//一般的接受者和接受者
}
顺便说一句,我认为您不需要使用@JsonCreator
进行反序列化。您遇到了什么错误?@VikramV当我试图发出POST请求时,我无法将JSON解组为数据请发布错误。如果非要我猜的话,那是因为params没有@JsonProperty。触发解组的代码也会很有用。请将其添加到问题中。我认为您的测试类缺少params属性。在测试类级别添加字段或添加@JsonIgnoreProperties(ignoreUnknown=true)。
class Test {
private String testName;
private Float expectedResult;
private List<Integer> params;
//general getters ans setters
}