2011-03-17 18 views
20

मैं एक jQuery AJAX पोस्ट को सर्वलेट में पोस्ट कर रहा हूं और डेटा JSON स्ट्रिंग के रूप में है। इसकी सफलतापूर्वक पोस्ट हो रही है लेकिन सर्वलेट पक्ष पर मुझे इन कुंजी-वैल जोड़े को सत्र ऑब्जेक्ट में पढ़ने और उन्हें स्टोर करने की आवश्यकता है। मैंने JSONObject क्लास का उपयोग करने का प्रयास किया लेकिन मैं इसे प्राप्त करने में सक्षम नहीं हूं।सर्विसलेट में JSON स्ट्रिंग पढ़ें

यहाँ कोड स्निपेट

$(function(){ 
    $.ajax(
    { 
     data: mydata, //mydata={"name":"abc","age":"21"} 
     method:POST, 
     url: ../MyServlet, 
     success: function(response){alert(response); 
    } 
}); 

सर्वलेट पक्ष

public doPost(HTTPServletRequest req, HTTPServletResponse res) 
{ 
    HTTPSession session = new Session(false); 
    JSONObject jObj = new JSONObject(); 
    JSONObject newObj = jObj.getJSONObject(request.getParameter("mydata")); 
    Enumeration eNames = newObj.keys(); //gets all the keys 

    while(eNames.hasNextElement()) 
    { 
     // Here I need to retrieve the values of the JSON string 
     // and add it to the session 
    } 
} 
+0

क्यों जावास्क्रिप्ट ऑब्जेक्ट mydata नहीं बना है जिसमें mydata.name, mydata.age शामिल है? फिर आप जेसन को डीकोड करने के बजाए अपने सर्वलेट में विशिष्ट पैरामीटर खींच सकते हैं? – dmcnelis

+0

मैं नहीं कर सकता कि कुंजी-वैल एक POST से दूसरे – sv1

+0

में भिन्न हो सकते हैं, जब आप अपने फ़ंक्शन में JQuery के साथ जेसन को डीकोड कर सकते हैं, fwiw। यह नहीं कह रहा कि आपका समाधान होना चाहिए, लेकिन यह वही काम पूरा करेगा। – dmcnelis

उत्तर

14

आप वास्तव में जेसन को पार्स नहीं कर रहे हैं।

JSONObject jObj = new JSONObject(request.getParameter("mydata")); // this parses the json 
Iterator it = jObj.keys(); //gets all the keys 

while(it.hasNext()) 
{ 
    String key = it.next(); // get key 
    Object o = jObj.get(key); // get value 
    session.putValue(key, o); // store in session 
} 
0

पर आप सिर्फ तब Jackson कोशिश एक मानचित्र में मार्शल करने के लिए चाहते हैं।

ObjectMapper mapper = new ObjectMapper(); 
... 
Map<String, Object> data = mapper.readValue(request.getParameter("mydata"), Map.class); 
2

तो मेरा उदाहरण यहां जाता है। मैंने जेसन का इस्तेमाल किया। जेएसओन्टोकनर ने मेरी स्ट्रिंग को टोकननाइज़ किया। (यहां से https://github.com/douglascrockford/JSON-java Json-जावा एपीआई)

String sJsonString = "{\"name\":\"abc\",\"age\":\"21\"}"; 
// Using JSONTokener to tokenize the String. This will create json Object or json Array 
// depending on the type cast. 
json.JSONObject jsonObject = (json.JSONObject) new json.JSONTokener(sJsonString).nextValue(); 

Iterator iterKey = jsonObject.keys(); // create the iterator for the json object. 
while(iterKey.hasNext()) { 
    String jsonKey = (String)iterKey.next(); //retrieve every key ex: name, age 
    String jsonValue = jsonObject.getString(jsonKey); //use key to retrieve value from 

    //This is a json object and will display the key value pair. 

    System.out.println(jsonKey + " --> " + jsonValue ); 
} 

आउटपुट:
उम्र -> 21
नाम -> एबीसी

17

अगर आप का उपयोग jQuery .ajax(), तो आप पढ़ने की जरूरत है HttpRequest इनपुट स्ट्रीम

StringBuilder sb = new StringBuilder(); 
    BufferedReader br = request.getReader(); 
    String str; 
    while((str = br.readLine()) != null){ 
     sb.append(str); 
    }  
    JSONObject jObj = new JSONObject(sb.toString()); 
संबंधित मुद्दे