0

My jQuery ajax sending data to a servlet and the data is in the form of JSON String. Its getting posted successfully but on the Servlet side I need to read these key-val pairs. I tried using JSONObject class but I am not able to get it.

Javascript

 var  all_urls = [{"url1":"This is aaaal 1"},{"url2":"This is bbbb2"},{"url3":"This is cccc3"}];

    $.ajax({
        type : 'POST',      
        dataType:'json',        
        data: { jsonData : JSON.stringify(all_urls ) }, 
        url : 'GetUserServlet',
        success : function(data) {                  
            $('#addText').text(data);
        },
        error : function(xhr, textStatus, errorThrown) { 
            alert(xhr.responseText);
        }
    });

Servlet

String myJsonData = request.getParameter("jsonData");

This is what I am getting using request.getParameter("jsonData") on the request object:

[{"url1":"This is aaaal 1"},{"url2":"This is bbbb2"},{"url3":"This is cccc3"}, {}...]

Need to parse the above JSON value then need to add some token along with values and send back as response.

[{"url1":"This is aaaal 1 & a=abc"},{"url2":"This is bbbb2 & a=xyz"},{"url3":"This is cccc3 & a= ijk"},{}...]

Any help appreciated!

Rajaguru
  • 19
  • 1
  • 4
  • You need to convert your `myJsonData` to `JsonObject` then iterate through to it to add new values .Here are some [examples](https://stackoverflow.com/questions/5245840/how-to-convert-jsonstring-to-jsonobject-in-java) and [here](https://stackoverflow.com/questions/32498872/how-to-append-key-values-in-json-object-using-java) as well. – Swati Apr 28 '20 at 06:27
  • In the abovelinked duplicate, Ctrl+F on "Manually sending" for correct approaches. – BalusC Apr 28 '20 at 10:57

0 Answers0