Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json simple #10

Open
uniquejava opened this issue Dec 25, 2015 · 0 comments
Open

json simple #10

uniquejava opened this issue Dec 25, 2015 · 0 comments

Comments

@uniquejava
Copy link
Owner

object to string

JSONObject result = new JSONObject();
result.put("ok", false);

if (id == null || id.trim().isEmpty()) {
    result.put("msg", "invalid parameter");
} else {
    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;

    try {
        conn = getConnection();

        pstmt = conn.prepareStatement("select order_no, order_amt from orders");
        rs = pstmt.executeQuery();

        JSONArray array = new JSONArray();

        while (rs.next()) {

            JSONObject o = new JSONObject();
            String orderNo = nvl(rs.getString("order_no"));
            String orderAmt = nvl(rs.getString("order_amt"));
            o.put("orderNo", orderNo);
            o.put("orderAmt", orderAmt);
            array.add(o);
        }
        result.put("ok", true);
        result.put("data", array);

    } catch (Exception e) {
        e.printStackTrace();
        result.put("msg", "Error get order:" + e.getMessage());

    } finally {
        cleanup(conn, pstmt, rs);
    }
}

renderJson(response, result);

renderJson()

protected void renderJson(HttpServletResponse response, JSONObject json) {
    String content = json.toJSONString();
    response.addHeader("Content-Type", "application/json");
    response.addHeader("Access-Control-Allow-Origin", "*");
    response.getWriter().print(content);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant