从Map中实例化JSONObject时出错:java.lang.NoSuchMethodError

时间:2015-10-11 15:19:51

标签: java json

我在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

我到处搜索,但我仍然收到此错误。

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

正如错误告诉您的那样,org.json.simple.JSONObject没有一个接受Map作为唯一参数的构造函数。

也许你的意思是使用org.json.JSONObject,它确实有这样的构造函数?

检查您在课程顶部导入的JSONObject