将JSON字符串从jsp传递到js文件

时间:2014-10-24 20:33:24

标签: javascript json jsp

所以,我在jsp文件中有一个json字符串(使用Gson)和我的数据库中的信息,我需要将该信息传递给js函数。

JSP(consulta.jsp):

<%@ page language="java" import="java.sql.*" %>
<%@ page language="java" import="db.Conexao" %>
<%@ page language="java" import="java.util.ArrayList" %>
<%@ page language="java" import="com.google.gson.Gson" %>

<%
try {
//instancia classe de conexao
Conexao conexao = new Conexao("localhost", "app", "root", "diogo");
//conecta no banco
Connection connection = conexao.connect();

//cria o statment e realiza a consulta
Statement st = connection.createStatement();
String sql = "SELECT * FROM crimes";
ResultSet rs = st.executeQuery(sql);

final ArrayList<String> id= new ArrayList<String>();

while(rs.next()) {
    id.add("id");
    id.add(rs.getString("id"));
    id.add("latitude");
    id.add(rs.getString("latitude"));
    id.add("longitude");
    id.add(rs.getString("longitude"));
}

String[] array = new String[id.size()];
array = id.toArray(array);

Gson gson = new Gson();
String json = gson.toJson(array);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);

//fecha a conexao com o banco
connection.close();
}
catch(Exception e) {
out.println(e.toString());
}

%>

JS功能:

function carregarPontos() {

$.getJSON('consulta.jsp', function(pontos) {

    $.each(pontos, function(index, ponto) {

        var marker = new google.maps.Marker({
            position: new google.maps.LatLng(LATITUDE VALUE HERE, LONGITUDE VALUE HERE),
            title: value,
            map: map,
            icon: 'arma2.png'
        });
    }
}

PS :.我的json字符串是[&#34; id&#34;,&#34; 1&#34;,&#34;纬度&#34;,&#34; -23.4831104000&#34;,&#34;经度&#34 ;,&#34; -46.6330227000&#34;&#34; ID&#34;&#34; 4&#34;&#34;纬度&#34;&#34; -23.5874328731&#34; &#34;经度&#34;&#34; -46.6573598700&#34]。这样对吗?谢谢

1 个答案:

答案 0 :(得分:1)

最好先制作一个更有用的数据结构。制作地图列表:

final ArrayList<HashMap<String, String>> id= new ArrayList<HashMap<String, String>>();

while(rs.next()) {
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("id", rs.getString("id"));
    map.put("latitude", rs.getString("latitude"));
    map.put("longitude", rs.getString("longitude"));
    id.add(map);
}

我很确定没有必要将ArrayList转换为普通数组; Gson应该能够直接吐出来:

Gson gson = new Gson();
String json = gson.toJson(id);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);

你将获得的JSON看起来像

[{"id": "1", "latitude": "-23.483", "longitude": "46.633"}, ... ]

因此您的JavaScript代码更容易实现:

$.getJSON('consulta.jsp', function(pontos) {

    $.each(pontos, function(index, ponto) {

        var marker = new google.maps.Marker({
            position: new google.maps.LatLng(ponto.latitude, ponto.longitude),
            title: value,
            map: map,
            icon: 'arma2.png'
        });
    }
});

请注意,Google Maps API可能希望您的lat / long值为数字,而不是字符串:

$.getJSON('consulta.jsp', function(pontos) {

    $.each(pontos, function(index, ponto) {

        var marker = new google.maps.Marker({
            position: new google.maps.LatLng(+ponto.latitude, +ponto.longitude),
            title: value,
            map: map,
            icon: 'arma2.png'
        });
    }
});