我的 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,
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
}
现在我遇到了异常
String json = "";
if(br != null){
json = br.readLine();
}
我得到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: Can not deserialize instance of java.util.ArrayList out of VALUE_STRING token at [Source: java.io.StringReader@f94ca; 行:1,列:27](通过引用链:com.ihexa.common.admin.cabsharing.action.Article["categories"])
我通过以下方式解决了答案
文章用户 = mapper.readValue(point, Article.class);
System.out.println(user.getRouteFirst());
Gson gson = 新 Gson();
TypeToken> token = new TypeToken>(){}; 列出 personList = gson.fromJson(user.getRouteFirst(), token.getType());
文章分类为
公共类文章{
private String routeFirst;
private String routeSecond;
//setter 和 getter
}
jsp jquery代码为
var article = new Object();
article.routeFirst = newRoute1;
article.routeSecond = newRoute2;
$.ajax({
type: 'POST',
url: '../..//admin/cabsharing/findIntersesctionPoint.do',
data : "point="+JSON.stringify(article),
dataType: 'json',
success: function(result) {
console.log("success");
},
error: function(e){
console.log("error");
}
});