在Servlet中消耗JSON对象的问题 [英] Problem consuming JSON object in a servlet
问题描述
这是我正在尝试做的事情,它非常简单,但是我陷入了困境:我正在尝试将JSP中形成的JSON对象发送到服务器端servlet并对其进行解析.
This is what i'm trying to do and its pretty simple but i'm getting stuck: I'm trying to send a JSON object formed in JSP to a server side servlet and parse it .
我到目前为止所做的事情:
What i've done till now:
- 构造了json.
- 将json发送到后端
$.ajax({
data: jsontosend,
url: 'MYSERVLET?name=asdf',
success: function(res){
alert('posted');
}
})
问题:
- 此JSON的名称是什么,因此我可以使用request.getParameter()在servlet中获取它?
- 当我打印request.getParameterNames()时,我将参数名称作为JSON字符串本身获取,因此MYSERVLET中所有参数名称的输出看起来像这样
Parameter = name
Parameter = {"ticker":"asd","date":"asd","bucket":"300","entry":[{"type":"asd","indicator":"asd","condition":"asd"}],"exit":[{"type":"qwe","indicator":"qwe","condition":"qwe"}]}
任何人都有什么问题的主意吗?
Anyone have an idea as to what the problem is ?
我也尝试查看此此处关于stackoverflow的问题,但存在相同的问题那里.还有一个重复问题,它没有得到了答复.
Also i tried looking at this question here on stackoverflow but the same problem exists there too. Also there is a duplicate question which hasn't been answered .
帮助! :(
推荐答案
阅读 http://api.jquery.com/jQuery.ajax/#sending-data-to-server :
data选项可以包含以下形式的查询字符串 key1 = value1& key2 = value2,或格式为{key1:'value1',key2: 'value2'}.如果使用后一种形式,则数据将转换为 发送前使用jQuery.param()查询字符串.
The data option can contain either a query string of the form key1=value1&key2=value2, or a map of the form {key1: 'value1', key2: 'value2'}. If the latter form is used, the data is converted into a query string using jQuery.param() before it is sent.
因此,您应该使用
$.ajax({
data: {theNameOfTheParameter : jsontosend,
name : 'asdf'},
url: 'MYSERVLET',
success: function(res){
alert('posted');
}
})
并使用request.getParameter("theNameOfTheParameter")
获取JSON字符串.
and use request.getParameter("theNameOfTheParameter")
to get the JSON string.
这篇关于在Servlet中消耗JSON对象的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!