Java 如何将带有json的ajax读入servlet

Java 如何将带有json的ajax读入servlet,java,Java,我需要您的帮助将JSON从jQueryAjax读取到我的servlet。 我想解析来自对jsonObject的ajax调用的内部数据 我的代码获取facebook用户信息并将相关数据传递给json,然后向servlet发送AJAX调用。 如何在servlet请求中读取这些参数 var jsonUserInfo = []; jsonUserInfo.push({"id" : (response["id"] ? response["id"]

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

我的代码获取facebook用户信息并将相关数据传递给json,然后向servlet发送AJAX调用。 如何在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组织jar

谢谢,
我不知道JSON组织,但你只需要做
JSONObject-JSONObject=(JSONObject)(new-JSONParser().parse(jsonstring))
JSONArray JSONArray=(JSONArray)(新的JSONParser().parse(jsonstring))


在org.json中,这似乎是这样做的:
JSONObject-JSONObject=new-JSONObject(new-JSONTokener(jsonstring))
JSONArray-JSONArray=newjsonarray(newjsontokener(jsonstring))

我不知道JSON组织,但你只需要做
JSONObject JSONObject=(JSONObject)(新的JSONParser().parse(jsonstring))
JSONArray JSONArray=(JSONArray)(新的JSONParser().parse(jsonstring))


在org.json中,这似乎是这样做的:
JSONObject-JSONObject=new-JSONObject(new-JSONTokener(jsonstring))
JSONArray-JSONArray=newjsonarray(newjsontokener(jsonstring))

首先,托马斯谢谢你的回答。如果我使用:JSONObject JSONObject=new JSONObject(new JSONTokener(request.getParameter(“userInfo”)),如何获取“jsonstring”数据;我得到了一个错误。我在org.json.JSONTokener.more(JSONTokener.java:98)org.json.JSONTokener.next(JSONTokener.java:108)org.json.JSONTokener.nextClean(JSONTokener.java:162)org.json.JSONObject(JSONObject.java:178)得到了一个关于null对象的异常,我不是JavaScript专家,但我猜问题在于调用中的
contentType
字段,或者您应该将参数命名为
userInfo
,而不是
“userInfo”
。首先,感谢您的回答。如果我使用:JSONObject JSONObject=new JSONObject(new JSONTokener(request.getParameter(“userInfo”)),如何获取“jsonstring”数据;我得到了一个错误。我在org.json.JSONTokener.more(JSONTokener.java:98)org.json.JSONTokener.next(JSONTokener.java:108)org.json.JSONTokener.nextClean(JSONTokener.java:162)org.json.JSONObject(JSONObject.java:178)得到了一个关于null对象的异常,我不是JavaScript专家,但我猜问题在于调用中的
contentType
字段,或者您应该将参数命名为
userInfo
,而不是
“userInfo”