简体   繁体   中英

How to get object from http request in servlet?

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');}
    }
);

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/

;-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM