gpt4 book ai didi

java - 如何使用ajax读取json到servlet

转载 作者:太空宇宙 更新时间:2023-11-04 08:39:18 25 4
gpt4 key购买 nike

我需要您的帮助才能将 JSON 从 JQuery AJAX 读取到我的 servlet。我想解析来自 ajax 调用的内部数据到 jsonObject。

我的代码获取 facebook 用户信息并将相关数据传递给 json,然后将 AJAX 调用发送到 servlet。如何在 servlet 请求中读取这些参数?

 var jsonUserInfo = [];
jsonUserInfo.push({"id" : (response["id"] ? response["id"] : "wnull")});
jsonUserInfo.push({"first_name" : (response["first_name"] ? response["first_name"] : "wnull")});
jsonUserInfo.push({"last_name" : (response["last_name"] ? response["last_name"] : "wnull")});
jsonUserInfo.push({"username" : (response["username"] ? response["username"] : "wnull")});
jsonUserInfo.push({"birthday" : (response["birthday"] ? response["birthday"] : "wnull")});
jsonUserInfo.push({"gender" : (response["gender"] ? response["gender"] : "wnull")});
jsonUserInfo.push({"relationship_status": (response["relationship_status"] ? response["relationship_status"] : "wnull")});
jsonUserInfo.push({"timezone" : (response["timezone"] ? response["timezone"] : "wnull")});
jsonUserInfo.push({"locale" : (response["locale"] ? response["locale"] : "wnull")});
jsonUserInfo.push({"verified" : (response["verified"] ? response["verified"] : "wnull")});
jsonUserInfo.push({"updated_time": (response["updated_time"] ? response["updated_time"]: "wnull")});
jsonUserInfo.push({"interested_in": (response["interested_in"] ? response["interested_in"] : [])});
jsonUserInfo.push({"meeting_for": (response["meeting_for"] ? response["meeting_for"] : [])});

这是我的 ajax 调用:

$.ajax({ url: "MainController",
type:"POST",
data: ({ "action" : "setFacebookUserInfo",
"userInfo" : jsonUserInfo,
"servlet" : "UserProfile"
}),
dataType:"json",
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
success: function(data){
alert("here");
},
cache: true,
ifModified: true
});

如何使用 HttpServletRequest 解析 servlet 中的“jsonUserInfo”对象?我正在使用 JSON-org jar。

谢谢,伊多

最佳答案

我不知道 JSON-org 但有 JSON-simple你只需要做 JSONObject jsonobject = (JSONObject)(new JSONParser().parse(jsonstring));JSONArray jsonarray = (JSONArray)(new JSONParser().parse(jsonstring));

在 org.json 中,这似乎是按如下方式完成的: JSONObject jsonobject = new JSONObject(new JSONTokener(jsonstring));JSONArray jsonarray = new JSONArray(new JSONTokener(jsonstring));

关于java - 如何使用ajax读取json到servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5687250/

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