java 在javascript中创建json对象并将其发送到servlet
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14099403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating json object in javascript and sending it to servlet
提问by shabbir.rang
i am creating json object to save data and then sending it to the servlet. But when i try to retrieve the object and display its contents in java servlet, it throws me an error saying " A JSONObject text must begin with '{' at 1 [character 2 line 1]". I do not know how to display the json object in java which is been sent from javascript. Here's what i am doing:
我正在创建 json 对象来保存数据,然后将其发送到 servlet。但是,当我尝试检索对象并在 java servlet 中显示其内容时,它向我抛出一个错误,提示“JSONObject 文本必须以 '{' at 1 [character 2 line 1]”开头。我不知道如何在从 javascript 发送的 java 中显示 json 对象。这是我在做什么:
Javascript Code:
Javascript代码:
var arrayOfObjects = [];
arrayOfObjects.push({"divid":imageinc,"xCordinates":Xcord,"yCordinates":Ycord,"Height":canvasWidth,"Width":canvasHeight, "PageNo":pageNum});
Displaying Javascript contents:
显示 Javascript 内容:
for (var i = 0; i < arrayOfObjects.length; i++) {
var object = arrayOfObjects[i];
for (var property in object) {
alert(property + '=' + object[property]);
}
}
Sending object to servlet with jquery Ajax:
使用 jquery Ajax 将对象发送到 servlet:
var param = 'objarray=' +arrayOfObjects;
$.ajax({
url: '/ProjectName/finalXmlServGen',
type: 'POST',
dataType: 'json',
data: param,
success: function(result) {
alert('SUCCESS');
}
});
I get success message once i call the ajax. Now, i am receiving this object in servlet as:
一旦我调用 ajax,我就会收到成功消息。现在,我在 servlet 中接收这个对象为:
String objarray = request.getParameter("objarray").toString();
try {
JSONObject jsonObj = new JSONObject(objarray);
String xmlString= XML.toString(jsonObj);
System.out.println("JSON to XML: " + xmlString);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
The message i see in my console is the error " A JSONObject text must begin with '{' at 1 [character 2 line 1]". How do i parse the object and form an xml or string?
我在控制台中看到的消息是错误“JSONObject 文本必须以 '{' at 1 [character 2 line 1]”开头。我如何解析对象并形成一个 xml 或字符串?
回答by Denys Séguret
What you build isn't a "JSON object", but a plain javascript object.
您构建的不是“JSON 对象”,而是一个普通的 javascript 对象。
You must encode your object in JSON :
您必须在 JSON 中对您的对象进行编码:
var param = '?objarray=' + JSON.stringify(arrayOfObjects);
$.ajax({
url: '/ProjectName/finalXmlServGen'+param,
type: 'POST',
dataType: 'json',
success: function(result) {
alert('SUCCESS');
}
});
Or you may let jQuery do the encoding :
或者您可以让 jQuery 进行编码:
$.ajax({
url: '/ProjectName/finalXmlServGen',
type: 'POST',
dataType: 'json',
data: {objarray: arrayOfObjects}
success: function(result) {
alert('SUCCESS');
}
});
回答by Gimby
On the Java side of things you need to convert the JSON array to a Java POJO bean first to be able to (easily) do something with it. There are multiple APIs to do this; Google GSON and Hymanson are two possibilities.
在 Java 方面,您首先需要将 JSON 数组转换为 Java POJO bean,以便能够(轻松)使用它做一些事情。有多个 API 可以做到这一点;Google GSON和Hymanson是两种可能。
Hymanson is used internally by Jersey, the reference implementation of the JAX-RS API; it works very well in my experience.
Hymanson 由 Jersey 内部使用,JAX-RS API 的参考实现;根据我的经验,它非常有效。