Javascript 在您尝试JSON.parse之前,数据变量看起来像吗?像这样的对象{“productNo”:1,“name”:“cheese”,“price”:9.99}也许您的数据已经是对象,不需要解析,只需在console.log中尝试一件事data[0],看看您得到

Javascript 在您尝试JSON.parse之前,数据变量看起来像吗?像这样的对象{“productNo”:1,“name”:“cheese”,“price”:9.99}也许您的数据已经是对象,不需要解析,只需在console.log中尝试一件事data[0],看看您得到,javascript,jquery,json,ajax,servlets,Javascript,Jquery,Json,Ajax,Servlets,在您尝试JSON.parse之前,数据变量看起来像吗?像这样的对象{“productNo”:1,“name”:“cheese”,“price”:9.99}也许您的数据已经是对象,不需要解析,只需在console.log中尝试一件事data[0],看看您得到了什么{….但该表未打印我收到以下消息:未捕获的语法错误:意外标记{在JSON中尝试JSON.parse之前数据变量是什么样子的?对象类似于{“productNo”:1,“name”:“cheese”,“price”:9.99}可能您的数据已经


在您尝试JSON.parse之前,数据变量看起来像吗?像这样的对象{“productNo”:1,“name”:“cheese”,“price”:9.99}也许您的数据已经是对象,不需要解析,只需在console.log中尝试一件事data[0],看看您得到了什么{….但该表未打印我收到以下消息:未捕获的语法错误:意外标记{在JSON中尝试JSON.parse之前数据变量是什么样子的?对象类似于{“productNo”:1,“name”:“cheese”,“price”:9.99}可能您的数据已经是对象,不需要解析,只需尝试一件事数据[0]在like console.log中,查看您得到了什么。
 <script type="text/javascript">

 var form = $('#form1');

 form.submit(function () {
//event.preventDefault();
  $.ajax({
  type: form.attr('method'),
  url: form.attr('action'),
  data: form.serialize(),
 success: function (data) {
 var result=data;

  $('#result').html(result);
  // Result is a ID of a <div><div> in which I  
   //  print the json string. I want build a  
   // table here. But I want that this table is dinamic,   
   // that is also the NAME of columns must depends from json                                                             
  console.log(result);
     }
  });
  return false;
});  </script>
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        HttpSession session = request.getSession();

        String k = request.getParameter("choose"); 

        //From a dropdown select, for example I choose to print
        // the table "products"
         if(k.equals("products")){  
        ArrayList<Products> list = new ArrayList<Products>();
         list =  bDBean.listProducs();

         Iterator<Products> i = list.iterator();
                while(i.hasNext()){
                Products temp = i.next();
                Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
                String json = gson.toJson(temp);
               // response.setContentType("application/json");
                response.setContentType("text/html");
                response.setCharacterEncoding("UTF-8");
                response.getWriter().write(json);
                }
            }
          }
    <script type="text/javascript">

      $(function() {
        var form = $('#form1');

        form.submit(function() {
          //event.preventDefault();
          $.ajax({
            type: form.attr('method'),
            url: form.attr('action'),
            data: form.serialize(),
            success: function(data) {
              var table = '<table><thead><tr><td>productNo</td><td>name</td><td>price</td></tr></thead><tbody>'
              $.each(JSON.parse("["+data.split('}{').join('},{')+"]"), function(i, v) {
                table += '<tr><td>' + v.productNo + '</td><td>' + v.name +' < /td><td>'+v.price+'</td > < /tr>';
              });
              table += '</tbody></table>'
              $('#result').html(table);
            }
          });
          return false;
        });
      });

    </script>
success: function (data) {
var table = '<table><thead><tr><td>productNo</td><td>name</td><td>price</td></tr></thead><tbody>'
$.each(JSON.parse("["+data.split('}{').join('},{')+"]"),function(i,v){
      table+= '<tr><td>'+v.productNo+'</td><td>'+v.name+'</td><td>'+v.price+'</td></tr>';
});
table+='</tbody></table>'
$('#result').html(table);
}