如何防止在JSONOBject值中插入额外的引号?

时间:2015-08-18 13:23:56

标签: java arrays json

public JSONArray updatedGames() {
    JSONArray ar = new JSONArray();

    try {
        conn = connection.getConnection();
        String query = "CALL websocket_games(2)";
        CallableStatement custStmt = conn.prepareCall(query);
        rs = custStmt.executeQuery();

        while (rs.next()) {
            System.out.println("id "+rs.getString("our_game_id"));
            JsonProvider provider = JsonProvider.provider();
            JsonObject updatemessage = provider.createObjectBuilder()
                    .add("game_id", rs.getString("our_game_id"))
                    .add("date", rs.getString("dat"))
                    .build();
            ar.put(updatemessage);
        }
        if(ar.length()>0){
            System.out.println("provider "+ar.getString(0) );
        }


        rs.close();
        custStmt.close();
        conn.close();
    } catch (Exception e) {
        System.out.println(e);
    }
    System.out.println("number of games " + ar.length());
    return ar;
}

上述功能的输出为{"game_id":"\"8240\"","date":"\"2015-08-18\""}
但是我想要的输出是{"game_id":"8240","date":"2015-08-18"}

如何防止在JSONOBject值中插入那些额外的引号?

2 个答案:

答案 0 :(得分:0)

您的问题不在Java代码中。

您的数据库包含那些已引用值的字段,或者您的websocket_games存储过程会添加这些引号。

答案 1 :(得分:0)

    //------please try this instead of your while loop ----------
   JSONArray ar = new JSONArray();
   while (rs.next()){
         System.out.println("id "+rs.getString("our_game_id"));
         JsonObject updatemessage = new JsonObject();
         updatemessage.put("game_id", rs.getString("our_game_id"));
         updatemessage.put("date"   , rs.getString("dat"));
         ar.add(updatemessage);
   }

   if(ar.length()>0){
          System.out.println("provider "+ar.getString(0) );
   }

注意:请检查数据库字段值...