gpt4 book ai didi

java - jsp中如何显示Json对象

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:15:49 26 4
gpt4 key购买 nike

我是 jsp 新手。我有一个JSON编码

{"eventDetails":[{"startDate":1387175589253,"eventJSON":"{\"userIp\":\"***.***.***.**\",\"organizationUid\":\"4261-5404a609bd14\",\"userAgent\":\"Restlet-Framework\/2.1m3\",\"currentSessionToken\":\"9f3ee5b076-27dbead458ca\",\"endTime\":1312,\"apiKey\":\"b6bd9-30849cf17727\",\"startTime\":13853,\"sessionToken\":null,\"eventId\":\"4731d1-adb6-10faaa3ee269\",\"userUid\":null,\"userId\":1,\"context\":\"\/v2\/account\/loginas\/anonymous\",\"eventName\":\"user-loginAs\""

我需要将 eventJSON 对象显示为原始数据:

{\"userIp\":\"204.236.177.80\",\"organizationUid\":\"4261739e-ccae-11e1-adfb-5404a609bd14\",\"userAgent\":\"Restlet-Framework\/2.1m3\",\"currentSessionToken\":\"9f3ee54a-4f5f-b076-27d8ca\",\"endTime\":1387175589312,\"apiKey\":\"b6b8-4ad5-96d9-3084927\",\"startTime\":1387175589253,\"sessionToken\":null,\"eventId\":\"478e8-4ed1-adb6-10faaa3ee269\",\"userUid\":null,\"userId\":11878,\"context\":\"\/v2\/act\/lonas\/anonymous\",\"eventName\":\"user-loginAs\"}

如何使用 gson 显示 eventJSON 并使用 c:out 打印。

JSP 页面

<tr id="datalogList-${gC.index}">
<td id = "eventName-${gC.index}">
<c:out value="${datalog.eventName}" />
</td>
<td id = "startTime-${gC.index}">
<c:out value="${datalog.startTime}" />
</td>
<td id = "endTime-${gC.index}">
<c:out value="${datalog.endTime}" />
</td>
<td id = "apiKey-${gC.index}"></a>
<c:out value="${datalog.apiKey}" />
</td>
</tr>

Java

  Datalogging[]  logevents = (Datalogging[]) gson.fromJson(eventValueList.toString(),Datalogging[].class);

感谢阅读。

最佳答案

  1. 您将它作为一个 JavaScript 变量,如下所示:

    变种人 = [{ "name": "John Doe", "address": "Main Street 1"},{ "name": "Jane Doe", "address": "Baker Street 1"},{ "name": "Jack Doe", "address": "Church Street 1"} ];

我建议使用 jQuery 从中创建一个 HTML 表格。

<!DOCTYPE html>
<html lang="en">
<head>
<title>Test</title>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
var persons = [
{ "name": "John Doe", "address": "Main Street 1" },
{ "name": "Jane Doe", "address": "Baker Street 1" },
{ "name": "Jack Doe", "address": "Church Street 1" }
];
$(document).ready(function() {
var table = $('<table/>').appendTo($('#somediv'));
$(persons).each(function(i, person) {
$('<tr/>').appendTo(table)
.append($('<td/>').text(person.name))
.append($('<td/>').text(person.address));
});
});
</script>
</head>
<body>
<div id="somediv"></div>
</body>
</html>

2。您将它作为 Java String 变量,如下所示:

String jsonPersons = "["
+ "{ \"name\": \"John Doe\", \"address\": \"Main Street 1\" },"
+ "{ \"name\": \"Jane Doe\", \"address\": \"Baker Street 1\" },"
+ "{ \"name\": \"Jack Doe\", \"address\": \"Church Street 1\" }"
+ "]";

然后我建议使用 JSON 解析器从中获取列表,例如 Google Gson:

List<Person> persons = new Gson().fromJson(jsonPersons, new TypeToken<List<Person>>()       {}.getType());

Person 类看起来像这样:

public class Person {
private String name;
private String address;
// Add or generate getters/setters.
}

让 servlet 把它放在请求范围内,然后转发给 JSP 进行显示,如下所示:

request.setAttribute("persons", persons); 
request.getRequestDispatcher("/WEB-INF/persons.jsp").forward(request, response);

在 JSP 中,使用 JSTL 对其进行迭代:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
...
<table>
<c:forEach items="${persons}" var="person">
<tr>
<td>${person.name}</td>
<td>${person.address}</td>
</tr>
</c:forEach>
</table>

3 与 2) 相同,您将其作为 Java 变量,但您想通过 JSP 中的 Ajax 获取它。然后创建一个 Servlet 类,它在 doGet() 方法中执行以下操作:

response.setCharacterEncoding("UTF-8");
response.setContentType("application/json");
response.getWriter().write(jsonPersons);

然后通过 jQuery Ajax 调用它,并使用与 1 相同的回调)。

$(document).ready(function() {
var table = $('<table/>').appendTo($('#somediv'));
$.getJSON('url/to/servlet', function(persons) {
persons.each(function(i, person) {
$('<tr/>').appendTo(table)
.append($('<td/>').text(person.name))
.append($('<td/>').text(person.address));
});
});
});

关于java - jsp中如何显示Json对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20756970/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com