我正在使用gson的fromjson方法解析json字符串,当我将解析器作为独立的Java应用程序运行时,它可以工作,但是当我从EAR运行它时,出现此错误:
java.lang.RuntimeException:无法为类DBExecuter.Queryjson $ valIOConfigclass调用no-args构造函数。为此类型向Gson注册InstanceCreator可能会解决此问题。
感谢您提供有关此问题的任何帮助
json:
{
"code": "code1",
"query": "text",
"type": "10",
"_condqflg": "1",
"_pkgvarflg": "0",
"_IOSerial": "1|2|5",
"_ioConfig": [{
"_iosl": "1",
"_iokey": "text",
"_iotype": "I",
"_ioflag": "I"
},
{
"_iosl": "2",
"_iokey": "text",
"_iotype": "V",
"_ioflag": "I"
},
{
"_iosl": "3",
"_iokey": "text",
"_iotype": "I",
"_ioflag": "I"
},
{
"_iosl": "4",
"_iokey": "CLIENT_NUM",
"_iotype": "I",
"_ioflag": "1"
}
],
"_valcfg": [{
"_cfgsl": "1",
"_cfgstr": "text",
"_cfgioSl": "4"
},
{
"_cfgsl": "2",
"_cfgstr": "text",
"_cfgioSl": "3|1"
}
]
},
{
"code": "code2",
"query": "text",
"type": "10",
"_condqflg": "1",
"_pkgvarflg": "0",
"_IOSerial": "1|2|5",
"_ioConfig": [{
"_iosl": "1",
"_iokey": "text",
"_iotype": "I",
"_ioflag": "I"
},
{
"_iosl": "2",
"_iokey": "text",
"_iotype": "V",
"_ioflag": "I"
},
{
"_iosl": "3",
"_iokey": "text",
"_iotype": "I",
"_ioflag": "I"
},
{
"_iosl": "4",
"_iokey": "CLIENT_NUM",
"_iotype": "I",
"_ioflag": "1"
}
],
"_valcfg": [{
"_cfgsl": "1",
"_cfgstr": "text",
"_cfgioSl": "4"
},
{
"_cfgsl": "2",
"_cfgstr": "text",
"_cfgioSl": "3|1"
}
]
}
解析器类:
public class JSONparser extends DBImplementManager {
ArrayList<Queryjson> jsonParseResult = new ArrayList<Queryjson>();
HashMap<String, Queryjson> _querymap = new HashMap<>();
public LinkedHashMap<String,Queryjson> _pgmValmap =new LinkedHashMap<String,Queryjson>();
private JSONparser()
{
//load(pgmid);
}
private static JSONparser instance;
public static JSONparser getInstance()
{
if(instance==null)
{
synchronized(JSONparser.class){
if(instance==null)
{
instance=new JSONparser();
}
}
}
return instance;
}
public void load(String pgmid, String modid)
{
try {
InputStream input = Thread.currentThread()
.getContextClassLoader().getResourceAsStream(path);
Reader reader = new InputStreamReader(input, "UTF-8");
jsonParseResult = new Gson().fromJson(reader, new TypeToken<List<Queryjson>>(){}.getType());
System.out.println("json loading begin");
if (jsonParseResult != null ) {
for (Queryjson _query : jsonParseResult) {
_querymap.put(_query.getCode(), _query);
_pgmValmap.put(pgmid+"_"+_query.getCode(), _query);
System.out.println("Result: " + _query.getCode());
// _query.loadmaps();
}
}
System.out.println("json loading done");
} catch (Exception e) {
e.printStackTrace();
}
}
}
pojo类: 公共类Queryjson {
public Queryjson(){
}
private String code;
private String query;
private String type;
private String _condqflg;
private String _pkgvarflg;
private String _IOSerial;
private valIOConfigclass[] _ioConfig;
private valCFGclass[] _valcfg;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getQuery() {
return query;
}
public void setQuery(String query) {
this.query = query;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getcondqflg() {
return _condqflg;
}
public void setcondqflg(String _condqflg) {
this._condqflg = _condqflg;
}
public String getpkgvarflg() {
return _pkgvarflg;
}
public void setpkgvarflg(String _pkgvarflg) {
this._pkgvarflg = _pkgvarflg;
}
public String get_IOSerial() {
return _IOSerial;
}
public void set_IOSerial(String _IOSerial) {
this._IOSerial = _IOSerial;
}
public valIOConfigclass[] get_ioConfig() {
return _ioConfig;
}
public void set_ioConfig(valIOConfigclass[] _ioConfig) {
this._ioConfig = _ioConfig;
}
public valCFGclass[] get_valcfg() {
return _valcfg;
}
public void set_valcfg(valCFGclass[] _valcfg) {
this._valcfg = _valcfg;
}
public class valIOConfigclass {
private String _iosl;
private String _iokey;
private String _iotype;
private String _ioflag;
public valIOConfigclass()
{
}
public String get_iosl() {
return _iosl;
}
public void set_iosl(String _iosl) {
this._iosl = _iosl;
}
public String get_iokey() {
return _iokey;
}
public void set_iokey(String _iokey) {
this._iokey = _iokey;
}
public String get_iotype() {
return _iotype;
}
public void set_iotype(String _iotype) {
this._iotype = _iotype;
}
public String get_ioflag() {
return _ioflag;
}
public void set_ioflag(String _ioflag) {
this._ioflag = _ioflag;
}
}
public class valCFGclass{
private String _cfgsl;
private String _cfgstr;
private String _cfgioSl;
public valCFGclass(){
}
public String get_cfgsl() {
return _cfgsl;
}
public void set_cfgsl(String _cfgsl) {
this._cfgsl = _cfgsl;
}
public String get_cfgstr() {
return _cfgstr;
}
public void set_cfgstr(String _cfgstr) {
this._cfgstr = _cfgstr;
}
public String get_cfgioSl() {
return _cfgioSl;
}
public void set_cfgioSl(String _cfgioSl) {
this._cfgioSl = _cfgioSl;
}
}
}
答案 0 :(得分:0)
将内部类(valIOConfigclass和valCFGclass)标记为static