String getCity = request.getParameter("name1");
HttpSession session = request.getSession();
try {
    ConnectToDb db = new ConnectToDb();

    con =db.getConnect();
    pstmt = con.prepareStatement("select Name, CountryCode, District, Population from city where name =?");
    pstmt.setString(1, getCity);
    rs=pstmt.executeQuery();
    ArrayList<getCity> getinfo = new ArrayList<getCity>();

    while(rs.next()){
        getCity gc = new getCity();
        gc.setCity(rs.getString("name"));
        gc.setContryCode(rs.getString("CountryCode"));
        gc.setDistrict(rs.getString("district"));
        gc.setPopulation(rs.getString("population"));
        getinfo.add(gc);
    }

    JSONObject jobj = new JSONObject();
    jobj.put("city", getinfo);
    System.out.println(jobj);

} catch (Exception e) {
    System.out.println(e.getMessage());
}


输出:

{"city":[{"city":"Perm","district":"Perm","contryCode":"RUS","population":"1009700"}]}


但是当我在其他机器输出中执行时,相同的代码会得到如下内容:{"city":[{"com.getCity.@AF345E"}]}

为什么会这样呢?

最佳答案

com.getCity.@AF345E


它是一个“ getCity”对象,未实现“ toString”方法。检查您的代码。

10-06 16:04