简体   繁体   English

如何使用json将ajax读取到servlet

[英]how to read ajax with json to servlet

I need your help to read a JSON from JQuery AJAX to my servlet. 我需要您的帮助才能将JSON从JQuery AJAX读取到我的servlet。 I want to parse the internal data from the ajax call to jsonObject. 我想解析ajax调用jsonObject的内部数据。

My code get facebook user information and pass the relavent data to json, then send AJAX call to servlet. 我的代码获取facebook用户信息,并将相关数据传递给json,然后将AJAX调用发送给servlet。 How do I read these parameter in my servlet request? 如何在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"]  : [])});

And here is my ajax call: 这是我的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
    });

How do I parse 'jsonUserInfo' object in my servlet with HttpServletRequest? 如何使用HttpServletRequest解析Servlet中的“ jsonUserInfo”对象? I'm using JSON-org jar. 我正在使用JSON-org jar。

Thanks, Ido 谢谢,伊藤

I don't know about JSON-org but with JSON-simple you'd just do JSONObject jsonobject = (JSONObject)(new JSONParser().parse(jsonstring)); 我不了解JSON-org,但是使用JSON-simple,您只需要执行JSONObject jsonobject = (JSONObject)(new JSONParser().parse(jsonstring)); or JSONArray jsonarray = (JSONArray)(new JSONParser().parse(jsonstring)); JSONArray jsonarray = (JSONArray)(new JSONParser().parse(jsonstring));

In org.json this seems to be done as follows: JSONObject jsonobject = new JSONObject(new JSONTokener(jsonstring)); 在org.json中,这似乎是按照以下步骤完成的: JSONObject jsonobject = new JSONObject(new JSONTokener(jsonstring)); or JSONArray jsonarray = new JSONArray(new JSONTokener(jsonstring)); JSONArray jsonarray = new JSONArray(new JSONTokener(jsonstring));

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM