#随手记#fastjson 解析数据

来源:互联网 发布:淘宝推广能做运营么 编辑:程序博客网 时间:2024/06/02 01:33
发生错误,JSON.parseObject default constructor not found. class com.lottery.app.contractTemplet.FAQ错误原因,缺少默认构造函数,修改bean文件,添加默认构造方法,问题解决。
以下为错误案例:

public class FAQ {private String question;private String answer;private String id;public FAQ(String id,String question, String answer) {super();this.question = question;this.answer = answer;this.id = id;}public String getQuestion() {return question;}public void setQuestion(String question) {this.question = question;}public String getAnswer() {return answer;}public void setAnswer(String answer) {this.answer = answer;}public String getId() {return id;}public void setId(String id) {this.id = id;}}
JSONObject objK = new JSONObject();try {JSONArray arrayK = new JSONArray();FAQ f1 = new FAQ("01", "机器如何操作?", "请仔细阅读说明书,或致电我们!");arrayK.put(new JSONObject(JSON.toJSONString(f1)));FAQ f2 = new FAQ("02", "机器出现故障如何处理?", "请仔细阅读说明书,或致电我们!");arrayK.put(new JSONObject(JSON.toJSONString(f2)));objK.put("list", arrayK);contractTemplet.setContractFile(objK.toString());} catch (JSONException e1) {e1.printStackTrace();}String file = contractTemplet.getContractFile();try {JSONObject obj = new JSONObject(file);JSONArray array = obj.getJSONArray("list");for (int i = 0; i < array.length(); i++) {JSONObject te=array.getJSONObject(i);FAQ group2 = JSON.parseObject(te.toString(), FAQ.class);if (group2 == null) {return JsonUtil.getReturnJson(0, "常见问题错误");}}} catch (JSONException e) {e.printStackTrace();}catch (com.alibaba.fastjson.JSONException e) {//抛出异常!!!e.printStackTrace();}