我从Api得到了json。它包含所有值作为字符串。我根据计算中的要求转换了所有原语。有人告诉我当json返回null值时如何处理如何根据java字段强制转换它们。
{"student_answer":{"1" :"2","2":"3","3 ": "2"}}
其中第一个值显示答案号和第二个值。
Class Student_answer {
private String sQ1;
private String sQ2;
private String sQ3;
private String sQ4;
}
当所有值来自json时它工作正常,但是当json具有空值或&#34;&#34;值它显示异常如何处理它?</ p>
解析代码在这里:
final String uri = "http://localhost:8080/aakashv2/api";
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
ReportComingValues value=null;
try {
value=objectMapper.readValue(new URL(uri),ReportComingValues.class);
}catch(){}
public class ReportComingValues {
private Id id;
private Father_answer father_answer;
private Mother_answer mother_answer;
private Student_answer student_answer;
private String student_rollno;
}