开发者

how to read ajax with json to servlet

I need your help to read a JSON from JQuery AJAX to my servlet. I want to parse the internal data from the ajax call to jsonObject.

My code get facebook user information and pass the relavent data to json, then send AJAX call to servlet. How do I read these parameter in my servlet request?

 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({ 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? I'm using 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)); or JSONArray jsonarray = (JSONArray)(new JSONParser().parse(jsonstring));

In org.json this seems to be done as follows: JSONObject jsonobject = new JSONObject(new JSONTokener(jsonstring)); or JSONArray jsonarray = new JSONArray(new JSONTokener(jsonstring));

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜