与Java Servlet中的json对象分开获取值 [英] get value separately from json object in java servlet
本文介绍了与Java Servlet中的json对象分开获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的javascript代码,用于将json对象发送到servlet
this is my javascript code to send json object to servlet
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对象并分别获取数据.像这样
I want read json object from java servlet and get data separately. like this
String username = username from json object
String password = password from json object
String type = type from json object
我正在使用json-lib-2.4-jdk15.jar 请帮助我.
I'm using json-lib-2.4-jdk15.jar please help me..
推荐答案
在您的servlet中,在do[Get,Post]()
方法中执行以下操作
In your servlet, do the following in the do[Get,Post]()
method
JSONObject jsonObject = JSONObject.fromObject( request.getQueryString() );
String username= jsonObject.get( "username" );
String password= jsonObject.get( "password" );
String type= jsonObject.get( "type" );
您还需要从JS中删除以下行.
You also need the remove the following line from your JS.
request = JSON.parse(data);
这篇关于与Java Servlet中的json对象分开获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文