1
votes

I use jquery to send a json object servlet

It send the following data in the request


Form Dataview URL encoded stringArray[]:s1

stringArray[]:s2

stringArray[]:s3

objectArray[0][key1]:val1

objectArray[0][att1]:att val1

objectArray[1][key2]:val2

objectArray[1][att2]:att val2

objectArray[2][key3]:val3

objectArray[2][att3]:att val3


for the string array, I can use request.getParameterValues["stringArray"]

but the object array is cannot. How I can get the json array in the servlet?

var json={"stringArray":["s1", "s2", "s3"],
            "objectArray":[{"key1":"val1", "att1":"att val1"},
                            {"key2":"val2", "att2":"att val2"},
                            {"key3":"val3", "att3":"att val3"}]
        };
$.ajax
(
    {
        url:'../test',
        data:json,
        type:'post',
        cache:false,
        dataType:'json',
        success:function(data)
                {

                },
        error:function(){alert('error');}
    }
);
1
I think you should use a JSon library for that purpose, it will help you decode properly your parameter. GSon is one of them, Jackson is another one. There are surely others.Guillaume Polet

1 Answers

0
votes

You first need parse the JSON array in the servlet, then you can use this.

http://answers.oreilly.com/topic/257-how-to-parse-json-in-java/

;-)