net.sf.json转换List报错问题及解决方案
1,JSON字符串转换为List报错:
//修改前代码
JSONArray jsonArray=JSONArray.fromObject(jsonStr);
List<ComboboxJson> comboboxJsonList=(List<ComboboxJson>)JSONArray.toCollection(jsonArray,ComboboxJson.class);
[ERROR] net.sf.json.JSONException: java.lang.NoSuchMethodException
如图:
问题原因:
JSON字符串中字段值包含 null 会出现如上提示
解决方案:
null替换为 “”
jsonStr.replace(“null”,"""");
//修改后代码
JSONArray jsonArray=JSONArray.fromObject(jsonStr.replace("null","\"\""));
List<ComboboxJson> comboboxJsonList=(List<ComboboxJson>) JSONArray.toCollection(jsonArray,ComboboxJson.class);