Java 将数组从Jquery发送到Struts1方法
我的jsp和jquery代码如下Java 将数组从Jquery发送到Struts1方法,java,jquery,ajax,json,org.json,Java,Jquery,Ajax,Json,Org.json,我的jsp和jquery代码如下 var article = new Object(); article.title = "abc"; article.url = "abc"; article.categories = [1,2,3]; article.tags = [1,2,3]; console.log('hi'); $.ajax({ type: 'POST', url: URL, conten
var article = new Object();
article.title = "abc";
article.url = "abc";
article.categories = [1,2,3];
article.tags = [1,2,3];
console.log('hi');
$.ajax({
type: 'POST',
url: URL,
contentType:"application/json",
data: JSON.stringify(article),
dataType: 'json',
success: function(result) {
console.log(result);
},
error: function(e){
alert('Error in Processing');
}
});
我的java代码是
BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
String json = "";
if(br != null){
json = br.readLine();
}
// 2. initiate jackson mapper
ObjectMapper mapper = new ObjectMapper();
//mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
// 3. Convert received JSON to Article
Article article = mapper.readValue(json, Article.class);
现在我的课堂是
public class Article {
private String title;
private String url;
private List<String> categories;
private List<String> tags;
//getters and setters
}
我得到的json如下
{"title":"abc","url":"abc","categories":"[1, 2, 3]","tags":"[1, 2, 3]"}
事实上应该是这样的
{"title":"abc","url":"abc","categories":[1, 2, 3],"tags":[1, 2, 3]}
我不明白发生了什么事
因此我得到了一个例外
com.fasterxml.jackson.databind.JsonMappingException:无法反序列化java.util.ArrayList的实例超出值\u字符串标记
在[来源:java.io。StringReader@f94ca;行:1,列:27](通过引用链:com.ihexa.common.admin.cabsharing.action.Article[“categories”])
我用下面的方法解出了答案
Article user=mapper.readValue(点,Article.class)
System.out.println(user.getRouteFirst())
Gson Gson=新的Gson()
TypeToken>token=新的TypeToken>(){};
List personList=gson.fromJson(user.getRouteFirst(),token.getType())
文章类别为
公共类文章{
private String routeFirst;
private String routeSecond;
//二传手和接球手
}
jspjQuery代码
我的
jsp
将
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Calculator</title>
</head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script type="text/javascript">
$( document ).ready(function() {
//alert("DOM is ready");
});
function sendJsonData() {
var article = new Object();
article.title = "abc";
article.url = "abc";
article.categories = [1,2,3];
article.tags = [1,2,3];
//alert("JSON string :"+ JSON.stringify(article));
$.ajax({
type: 'POST',
url: "JsonServlet",
//contentType:"application/json",
//data: {point:point},
data : "point="+encodeURIComponent(JSON.stringify(article)),
dataType: 'json',
success: function(result) {
},
error: function(e){
//alert('Error in Processing');
}
});
}
</script>
<body>
<button id="jsonButton" onclick="sendJsonData()">send jdon Data</button>
</body>
</html>
public class JsonServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String point = request.getParameter("point");
System.out.println("Point : " + point );
if(point != null){
ObjectMapper mapper = new ObjectMapper();
try {
// read from string, convert it to Article class object
Article user = mapper.readValue(point, Article.class);
// Conver the Article class object in to the JSON string
System.out.println("Output Json String is :::::::::::> "+mapper.writeValueAsString(user));
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
这是我在控制台中得到的输出
Point : {"title":"abc","url":"abc","categories":[1,2,3],"tags":[1,2,3]}
Json String is :::::::::::> {"title":"abc","url":"abc","categories":["1","2","3"],"tags":["1","2","3"]}
希望这有帮助。您能提供您的运行代码吗?我已经尝试过了,但我使用的是struts1和jsp框架将myJsonData设置为null。我将此作为输出点:{“title”:“abc”,“url”:“efg”,“categories”:“[1,2,3]”,“tags”:“[4,5,6]”}com.fasterxml.jackson.databind.JsonMappingException:无法反序列化java.util.ArrayList的实例,使其超出[Source:java.io]处的字符串标记的值。StringReader@1a37192;行:1,列:27](通过参考链:com.ihexa.common.admin.cabsharing.action.Article[“categories”])在servlet和jsp中使用代码时,它的工作方式与您使用动态web应用程序作为输出给我的文件类似,但您的问题是
仅将数组从Jquery发送到servlet
。因此,您必须关闭此线程并创建一个新线程。如果您觉得我的答案
很有用,您可以接受答案。我根据您的要求。请检查一下并告诉我。
public class JsonServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String point = request.getParameter("point");
System.out.println("Point : " + point );
if(point != null){
ObjectMapper mapper = new ObjectMapper();
try {
// read from string, convert it to Article class object
Article user = mapper.readValue(point, Article.class);
// Conver the Article class object in to the JSON string
System.out.println("Output Json String is :::::::::::> "+mapper.writeValueAsString(user));
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Point : {"title":"abc","url":"abc","categories":[1,2,3],"tags":[1,2,3]}
Json String is :::::::::::> {"title":"abc","url":"abc","categories":["1","2","3"],"tags":["1","2","3"]}