anupama jirange
anupama jirange

Reputation: 652

How to return list as JSON object from jsp in ajax?

This is my jsp file...

    <%@page import="java.util.*,java.util.List,java.util.ArrayList"%>
    <%@ page import="java.io.*,java.sql.*,java.text.*,pis.util.*"%>
    <%@ page  contentType="application/json; charset=UTF-8" pageEncoding="UTF-8"%>
    <%
        response.setContentType("application/json");
        String LedgerNo=request.getParameter("LedgerNo").trim();
        ResultSet rs = dm.getData("SELECT SaleNo , DisplayPrefix ,DisplayNo FROM sale 
           where CustomerName      like '"+ LedgerNo +"'");

        List<Map<String, Object>> menuList = new ArrayList<Map<String, Object>>();

       while (rs.next()) {
           System.out.println("SaleNo.."+rs.getInt(1));
           System.out.println("DisplayNo.."+rs.getString(2) +" "+ rs.getString(3));

           Map<String, Object> menuMap = new HashMap<String, Object>();
           menuMap.put("SaleNo",rs.getInt(1));
           menuMap.put("DisplayNo",rs.getString(3) );
           menuList.add(menuMap);

        } 

        System.out.println("menuList.."+menuList.toString());
        out.println(menuList);
        rs.close();
%>

I got my list as...

[{SaleNo=1, DisplayNo=K 1}, {SaleNo=2, DisplayNo=KC 1}]

I want to return this list as JSON . But it returns error,how can i achieve this?

Upvotes: 0

Views: 1509

Answers (1)

TedTrippin
TedTrippin

Reputation: 3667

You need to add quotes around Strings and use colon instead of equals. You don't need quotes around numbers.

your JSON should look like this...

[{"SaleNo":1, "DisplayNo":"K 1"}, {"SaleNo":2, "DisplayNo":"KC 1"}]

Upvotes: 1

Related Questions