2015-11-13 46 views
0

這是我的servlet:json數組有對象「esquina」。埃斯基納有兩個屬性,雙重coordX雙coordY我怎麼能從json數據從java servlet到jquery

package servlets; 

@WebServlet("/Mapa") 
public class ServletMapa extends HttpServlet { 
    private static final long serialVersionUID = 1L; 

    /** 
    * @see HttpServlet#HttpServlet() 
    */ 
    public ServletMapa() { 
     super(); 
     // TODO Auto-generated constructor stub 
    } 


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 

     Sistema instanciaSys = Sistema.darInstancia(); 
     instanciaSys.inicializarSistema(6); 
     Esquina[] esquinas = instanciaSys.getEsquinas();  
     JSONArray json =new JSONArray(); 

     JSONObject jO = null; 
     for (Esquina esquina : esquinas) { 
      jO = new JSONObject(esquina);    
      json.put(jO); 

      System.out.println(json);   
     } 


     request.setAttribute("esquinas", esquinas);  
     request.setAttribute("json", json);  


     request.getRequestDispatcher("/gui/Mapa.jsp").forward(request, response);  
    } 


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 

     doGet(request, response); 
    } 

} 

我需要從jsonArray獲取數據到jQuery的,我試過的getJSON()函數,但沒有工作。

here's代碼

function cargarMarcadores() { 

    var x=$("#iniSistema"); 
    x.click(function(){ 
       $.getJSON('localhost:8080/Carpuleame/Mapa',function(data){ 

        alert("data"); 
       }); 
      }); 

} 

有antoher辦法做到這一點?

回答

0

你可能想是這樣的:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 

    Sistema instanciaSys = Sistema.darInstancia(); 
    instanciaSys.inicializarSistema(6); 
    Esquina[] esquinas = instanciaSys.getEsquinas();  
    JSONArray json =new JSONArray(); 

    for (Esquina esquina : esquinas) { 
     JSONObject jO = new JSONObject(); 
     jO.put("coordX", esquina.getCoordX());    
     jO.put("coordY", esquina.getCoordY());    
     json.put(jO); 

    } 

    // tell the client that JSON is coming 
    response.setContentType("application/json"); 
    resposne.setCharacterEncoding("UTF-8"); 
    json.write(response.getWriter()); 
} 

有一大堆的會帶走大量的代碼的「蠻力」 -iness的框架,但是這是非常接近你的起始位置。

+0

謝謝,我需要這些coordenates來放置一個標記(谷歌地圖api)。 –

+0

很酷。如果我解決了您的問題,請考慮接受答案。 – bcholmes

相關問題