这是我将json对象发送到servlet的javascript代码
username = "Nash";
password = "619here";
type = "all";
data = '{ "username": "' + username + '", "password": "' + password + '", "type": "' + type + '" }';
request = JSON.parse(data);
$.getJSON(url, request, function (response) {
$("#result").append("<h1>Success</h1>");
$.each(response.vehicle, function (no, vehicle) {
$.each(vehicle, function (key, value) {
$("#result").append("<h2>" + key + " : " + value + "</h2>");
});
$("#result").append("<br>");
});
})
我想从java servlet读取json对象并分别获取数据。像这样
String username = username from json object
String password = password from json object
String type = type from json object
我正在使用json-lib-2.4-jdk15.jar 请帮帮我..
答案 0 :(得分:0)
在您的servlet中,在do[Get,Post]()
方法
JSONObject jsonObject = JSONObject.fromObject( request.getQueryString() );
String username= jsonObject.get( "username" );
String password= jsonObject.get( "password" );
String type= jsonObject.get( "type" );
您还需要从JS中删除以下行。
request = JSON.parse(data);