简体   繁体   English

Jquery 带有 Servlet 以获取数据库 JSON 数组 object

[英]Jquery with Servlet to fetch database JSON array object

Stackoverflow team members. Stackoverflow 团队成员。

I am struggling to get the solution of my problem.我正在努力解决我的问题。 Actually in my application I want to make use of Jquery and Json with Servlet.实际上,在我的应用程序中,我想通过 Servlet 使用 Jquery 和 Json。 In my Application There is one JSP Servlet page to enter detail entry like user_name , user_address etc.在我的应用程序中有一个 JSP Servlet 页面可以输入详细条目,例如user_nameuser_address等。

All this data will be send to database using jquery ajax.所有这些数据将使用 jquery ajax 发送到数据库。 Now I want to retrieve all records that are inserted to database using json in the form of json array object.现在我想检索使用 json 以 json 数组 object 的形式插入到数据库的所有记录。

I am able to insert record to database but I don't know how to get them back from database to json object array so i can use them again.我能够将记录插入数据库,但我不知道如何将它们从数据库中恢复到 json object 数组,以便我可以再次使用它们。 in some another jsp servlet page.在另一个 jsp servlet 页面中。

Help me solve my problem.帮我解决我的问题。

Best Regards此致
Yogendra约根德拉

First of all you need to do an Ajax call to your servlet, see following code:首先,您需要对您的 servlet 进行 Ajax 调用,请参见以下代码:

    $.getJSON("yourServlet", function(json) {
    alert("JSON Received Data: " + json);
    //Logic to Parse the received JSON
    });
   </script>

Secondly construct JSON object at server side with its specific format, something like:其次在服务器端构造 JSON object ,格式如下:

{
    "firstName": "John",
    "lastName": "Smith",
    "address": {
        "streetAddress": "21 2nd Street",
        "city": "New York",
        "state": "NY",
        "postalCode": 10021
    },
    "phoneNumbers": [
        "212 732-1234",
        "646 123-4567"
    ]
}

Now Construct a list of inserted records in the database, see following sample code that showing how to construct list of records in JSON:现在在数据库中构造插入记录列表,请参见以下示例代码,该示例代码显示了如何构造 JSON 中的记录列表:

List mybeanList = new ArrayList();
mybeanList.add(myBean1);
mybeanList.add(myBean2);

JSONArray jsonArray = JSONArray.fromObject(mybeanList);
System.out.println("==== : "+jsonArray);

Map map = new HashMap();
map.put("beanlist", jsonArray);

JSONObject jsonObject = JSONObject.fromObject(map);
return jsonObject;

Finally parse the received JSON response inside your jsp (using javascript or any other alternatives)...最后在 jsp 中解析收到的 JSON 响应(使用 javascript 或任何其他替代方案)...

Go through with this tutorial , if you face any trouble with JSON. Go 通过本教程,如果您遇到 JSON 的任何问题。

Well you need to establish a connection with your database server side, pull the records out of the database ( possibly filtering based on data from the ajax request? look at data attribute for .getJSON ) then your server needs to format this in JSON.那么您需要与您的数据库服务器端建立连接,将记录拉出数据库(可能基于来自 ajax 请求的数据进行过滤?查看.getJSONdata属性)然后您的服务器需要将其格式化为 JSON。 In PHP you'll use json_encode($data_array) -- then simply echo this back to the client.在 PHP 中,您将使用json_encode($data_array) - 然后简单地将其echo显给客户端。 For the jQuery request:对于 jQuery 请求:

   $.getJSON('http://site.com/ajax/get-latest-posts', {
         success: function(jsonObject) {
             // jsonObject[0].author, for example.
         }
   });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在servlet中获取jQuery JSON请求参数? - How to fetch jQuery JSON request parameters in servlet? Java servlet不将JSON对象返回给jquery ajax - Java servlet not returning JSON Object to jquery ajax 如何使用jquery将json数组发布到java servlet - How to post a json array to java servlet with jquery 如何使用JSON,JQuery,Servlet提取所有值? - how to fetch all the values using JSON,JQuery,Servlet? 如何在servlet中使用json对象返回项目数组以及如何使用jquery显示 - How return array of items using json object in servlet and how to display using jquery 如何通过数据库调用ajax调用,使用Java servlet,jquery,json对象从数据库中创建自动完成文本框,从而在html页面中 - How to create Autocomplete textbox from database results in a html page using java servlet, jquery, json object by making an ajax call 通过Jquery GET调用将JSON对象传递给Java Servlet - Pass JSON Object through Jquery GET call to Java Servlet 我们如何将json对象从servlet传输到jsp页面,以及如何在jsp页面中获取json数据? - How can we transfer json object from servlet to a jsp page and how to fetch that json data in jsp page? 在servlet中创建json对象 - Create json object in servlet 在jquery中使用来自servlet的json获取两个不同变量的数组 - Get two array on different variable using json from servlet in jquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM