我在Java
中使用 JSONObject 时收到错误。
我试图从Map
实例化 JSONObject :
Collection<Faction> factions = FactionColl.get().getAll();
for(Faction f : factions) {
String fac_id = f.getId();
ResultSet count = this.db.query("SELECT COUNT(*) AS count FROM helu_fac WHERE fac_id='" + fac_id + "'");
int exist = 0;
while(count.next()) {
exist = count.getInt("count");
}
if(exist == 0) {
Connection conn = this.db.getConnection();
PreparedStatement statement = conn.prepareStatement("INSERT INTO helu_fac VALUES(?,?,?,?,?,?,?)");
statement.setInt(1, 0);
statement.setString(2, fac_id);
statement.setString(3, f.getName().toLowerCase());
statement.setString(4, f.getDescription());
statement.setString(5, new JSONObject(f.getRelationWishes()).toJSONString());
statement.setDouble(6, f.getPower());
statement.setInt(7, 1);
statement.executeUpdate();
conn.close();
} else {
Connection conn = this.db.getConnection();
PreparedStatement ps = conn.prepareStatement(
"UPDATE helu_fac SET "
+ "fac_id = ?,"
+ "name = ?,"
+ "description = ?,"
+ "relations = ?,"
+ "power = ?"
+ " WHERE fac_id = \"" + fac_id +"\"");
ps.setString(1, f.getId());
ps.setString(2, f.getName().toLowerCase());
ps.setString(3, f.getDescription());
ps.setString(4, new JSONObject(f.getRelationWishes()).toJSONString());
ps.setString(5, String.valueOf(f.getPower()));
ps.executeUpdate();
conn.close();
}
}
我收到以下错误:
java.lang.NoSuchMethodError: org.json.simple.JSONObject: method <init>(Ljava/util/Map;)V not found
我到处搜索,但我仍然收到此错误。
感谢您的帮助!
答案 0 :(得分:1)
正如错误告诉您的那样,org.json.simple.JSONObject
没有一个接受Map
作为唯一参数的构造函数。
也许你的意思是使用org.json.JSONObject
,它确实有这样的构造函数?
检查您在课程顶部导入的JSONObject
。