jQuery 在jsp中创建一个json对象并将其与JQuery一起使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9046331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:15:27  来源:igfitidea点击:

Creating a json object in jsp and using it with JQuery

jqueryjsonjsp

提问by Decrypter

I've created a JSP application, which gets results based on a user search (using lucene). I store the results in a Bean.

我创建了一个 JSP 应用程序,它根据用户搜索(使用 lucene)获取结果。我将结果存储在 Bean 中。

I'm also using Jquery Ajax to display the results.

我也在使用 Jquery Ajax 来显示结果。

$.ajax({
    url : "search.jsp",
    data : "search=test",
    success : function(html) {
        ("#search_results").hide().html(html).fadeIn(1500);
    }
});

search.jsp

搜索.jsp

for (int i = 0; i < size; i++) {
    out.println(searchResult.get(i).getHTML());
}

This is working fine, however I want to change it so it returns a JSON object to JQuery and then let JQuery parse the objects and display the results

这工作正常,但是我想更改它,以便它向 JQuery 返回一个 JSON 对象,然后让 JQuery 解析对象并显示结果

I am not sure how to do this as I'm new to JSON objects and JSP. I could possibly do something like

我不知道如何做到这一点,因为我是 JSON 对象和 JSP 的新手。我可能会做类似的事情

JSONObject json = new JSONObject();
json.put("title", "TITLE_TEST");
json.put("link", "LINK_TEST");

but I dont know how to return jsonto jquery then let jquery parse the objects

但我不知道如何返回jsonjquery 然后让 jquery 解析对象

Any help is appreciated :)

任何帮助表示赞赏:)

回答by Darin Dimitrov

Here's an exampleyou may take a look at. Basically your JSP page might look like this:

这是一个您可以查看的示例。基本上您的 JSP 页面可能如下所示:

<%@page contentType="text/html; charset=UTF-8"%>
<%@page import="org.json.simple.JSONObject"%>
<%
    JSONObject json = new JSONObject();
    json.put("title", "TITLE_TEST");
    json.put("link", "LINK_TEST");
    out.print(json);
    out.flush();
%>

and on the client:

在客户端:

$.ajax({
    url : 'search.jsp',
    data : { search: 'test' },
    dataType: 'json',
    success : function(json) {
        alert(json.title);
    }
});

And here are even more examples.

这里还有更多的例子

回答by Jashwant

Ultimately its being trasnferred over http. So, creating a json object wont do much help.

最终它是通过 http 传输的。因此,创建一个 json 对象不会有太大帮助。

I am not a java expert but you can create a simple string which matches with json structure and then parse it on client side.

我不是 Java 专家,但您可以创建一个与 json 结构匹配的简单字符串,然后在客户端解析它。

Like

喜欢

string s =  { "title": "testTitle", "link" : "testLink"}
out.println(s)

This will do the trick.

这将解决问题。

Edit: by seeing Darin's answer,

编辑:通过看到达林的回答,

Include this on you java code,

将此包含在您的 Java 代码中,

<%@page contentType="application/json; charset=UTF-8"%>

回答by nightmareTomek

This worked for me:

这对我有用:

%>
String json = "{ \"title\": \"testTitle\", \"link\" : \"testLink\"}";
response.getWriter().write(json);
response.getWriter().flush();
response.getWriter().close();
<%

I used it to feed an easyui-datagrid. response.getWriter().write(json)worked, but out.println(json)did not though he didn't throw any exceptions. Also the inner quotes must be double as well, so it becomes necessary to mask them with `\".

我用它来提供一个easyui-datagrid。response.getWriter().write(json)工作,但out.println(json)没有,尽管他没有抛出任何异常。此外,内部引号也必须是双引号,因此有必要用“\”来掩盖它们。

回答by Varun Ved

Pretty simple approach would be to use taglib - json something like this :

非常简单的方法是使用 taglib-json 是这样的:

<%@ taglib prefix="json" uri="http://www.atg.com/taglibs/json" %>

Then you can use json tags to create it out of list:

然后你可以使用 json 标签从列表中创建它:

<json:array items="${someObject.someList}" var="oneRow">
<json:object>
    <json:property name="username" value="${oneRow.username}"/>
    <json:property name="password" value="${oneRow.password}"/>
    <json:property name="email" value="${oneRow.email}"/>
</json:object>

Above jsp when executes will O/P following :

以上jsp执行时会O/P如下:

[
 {"username":"varun","password":"*****","email":"johndoe@sssdotcom"},
 {"username":"ved","password":"*****","email":"johndoe1@sssdotcom"},
 {"username":"von","password":"*****","email":"johndoe2@sssdotcom"}
]

Thats all folks!

这就是所有人!