gpt4 book ai didi

java - 如何从jsp接收并解析json数据?

转载 作者:行者123 更新时间:2023-12-01 09:24:18 26 4
gpt4 key购买 nike

我从servlet发送一个json,就像这样

User profileUser = userService.get(id);
request.setAttribute("profileUser", profileUser);
JSONObject json = new JSONObject();
try {
json.put("profileUser", profileUser);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.setContentType("application/json");
response.getWriter().write(json.toString());

并在ajax调用中的javascript

$.ajax({
cache : false,
type : "GET",
url : "UserServlet?id=" + userId,
success : function(result) {
alert(result.profileUser);
}
});

警报的结果是给我 Entity.user 但如果我想调用 result.profileUser.uuid 或该用户的属性返回给我 undefined 。有人可以帮我吗?

最佳答案

如果您要将 json 发送到 Ajax 客户端,那么您需要指定您要接收 json 类型。像这样:

$.ajax({
cache : false,
type : "GET",
dataType : "json",
url : "UserServlet?id=" + userId,
success : function(result) {
alert(result.profileUser);
}
});

这将导致您的 success 方法将结果参数评估为 json 对象。

关于java - 如何从jsp接收并解析json数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39961887/

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