Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/458.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在jsp中创建json对象?_Javascript_Json_Jsp_Servlets - Fatal编程技术网

Javascript 如何在jsp中创建json对象?

Javascript 如何在jsp中创建json对象?,javascript,json,jsp,servlets,Javascript,Json,Jsp,Servlets,我创建了一个带有少量输入字段的JSP页面。当我单击submin时,我需要javascript来创建json对象并在其中输入值,我希望在同一个jsp页面中显示json数据 这是我的表格 <form name="jsond" method="get"> <br> <p id="msg"></p> First Name: <input type="text" id="firstName"/><br> Last Name: <

我创建了一个带有少量输入字段的JSP页面。当我单击submin时,我需要javascript来创建json对象并在其中输入值,我希望在同一个jsp页面中显示json数据

这是我的表格

<form name="jsond" method="get">
<br>
<p id="msg"></p>
First Name:
<input type="text" id="firstName"/><br>
Last Name:
<input type="text" id="lastName"/><br>
E-mail:
<input type="text" id="email"/><br>
Mobile No:
<input type="text" id="mobile"/><br>
Place:
<input type="text" id="place"/><br>
Country:
<input type="text" id="country"/><br>
<br>
<input type="submit" name="submit" value="Submit" onclick="return submitForm(this, event);">
</form>


名字:
姓氏:
电邮:
手机号码:
地点:
国家:

这是我的剧本

function submitForm(thisObj, thisEvent) {
       var firstName = document.forms["jssond"]["firstName"].value;
       var lastName = document.forms["jssond"]["lastName"].value;
       var email = document.forms["jssond"]["email"].value;
       var mobile = document.forms["jssond"]["mobile"].value;
       var place = document.forms["jssond"]["place"].value;
       var country = document.forms["jssond"]["country"].value;

// How to do the remaining code? I want to store the above variables in json object and display json values in <p id="msg"></p>
函数提交表单(thisObj,thisEvent){
var firstName=document.forms[“jssond”][“firstName”].value;
var lastName=document.forms[“jssond”][“lastName”].value;
var email=document.forms[“jssond”][“email”].value;
var mobile=document.forms[“jssond”][“mobile”].value;
var place=document.forms[“jssond”][“place”].value;
var country=document.forms[“jssond”][“country”].value;
//如何处理剩余的代码?我想将上述变量存储在json对象中,并在

如何将json对象从javascript传递到servlet

感谢

Well与带有键值对的JavaScript对象非常相似,它比您想象的要简单:)

以以下形式提交JSON:

// Assuming 'request' is HttpServletRequest

String jsonString = request.getParameter("jsonData");
JSONObject jsonObject = (JSONObject) JSONValue.parse(jsonString);
要以
submit
的形式提交JSON数据:

假设在表单中有一个隐藏字段,用于保存JSON字符串值:

<input type=hidden" id="jsonData" name="jsonData" />
处理JSON服务器端:

// Assuming 'request' is HttpServletRequest

String jsonString = request.getParameter("jsonData");
JSONObject jsonObject = (JSONObject) JSONValue.parse(jsonString);
在处理表单提交操作的服务器端代码中,比如说在PHP(在您使用的任何语言中,可能都有一些等价物):

在Java中:

// Assuming 'request' is HttpServletRequest

String jsonString = request.getParameter("jsonData");
JSONObject jsonObject = (JSONObject) JSONValue.parse(jsonString);

您可以在javascript中创建对象,然后使用
JSON.stringify
将JSON创建为文本

function submitForm(thisObj, thisEvent) {
   var object = {
       firstName: document.forms["jssond"]["firstName"].value,
       lastName: document.forms["jssond"]["lastName"].value,
       email: document.forms["jssond"]["email"].value,
       mobile: document.forms["jssond"]["mobile"].value,
       place: document.forms["jssond"]["place"].value,
       country: document.forms["jssond"]["country"].value
   };
   document.getElementById('msg').innerHTML = JSON.stringify(object);
}

你应该查阅最新的javascript/json教程。
document.forms[etc]
大约15年前就被弃用了。

不,JSON不是一个JavaScript对象。JSON是一种文本数据表示,就像XML一样。要从对象创建JSON,必须调用
JSON.stringify
。将JavaScript对象/数组文本称为JSON是一个常见错误,但这是错误和混乱的,特别是对于初学者。@FelixKling JSON格式基于JavaScript。我并不是说JSON是JavaScript中的一个对象。我只是说两者的表示形式几乎相同。在这个问题的上下文中,
JSON.stringify
将是下一步,如果我们想将JSON表示为字符串。这将是
{“firstname”:firstname,“lastName”:lastName,“email”:email,“mobile”:mobile,“place”:place,“country”:country}
(但用实际值代替变量名)。但是他没有指定如何输出JSON,所以我们想如何表示它取决于他。是的,语法是相似的,但这并不意味着可以互换使用术语。事实上,你说过“JSON只是一个具有键值对的JavaScript对象"。您的
jsonObj
不是JSON,它只是一个对象。JSON在JavaScript中只能作为字符串存在。@SweetPase我想将JSON表示为stringJSON。JSON是一种数据格式,类似于ECMAScript
对象
初始值设定项所用的符号,并且与之兼容。它是一个结构化的字符串,可以解析为ECMAScript对象(情况并非相反)。唯一的“JSON对象”是(现在内置)解析和生成JSON内容的工具。您确定要创建JSON还是只创建JavaScript对象?请参阅MDN指南以了解对象:。我要创建JSON,而不是JavaScript variablerelated:您的方法没有意义。JSP表示JavaServer Pages.Java!=JavaScript。
// Assuming 'request' is HttpServletRequest

String jsonString = request.getParameter("jsonData");
JSONObject jsonObject = (JSONObject) JSONValue.parse(jsonString);
function submitForm(thisObj, thisEvent) {
   var object = {
       firstName: document.forms["jssond"]["firstName"].value,
       lastName: document.forms["jssond"]["lastName"].value,
       email: document.forms["jssond"]["email"].value,
       mobile: document.forms["jssond"]["mobile"].value,
       place: document.forms["jssond"]["place"].value,
       country: document.forms["jssond"]["country"].value
   };
   document.getElementById('msg').innerHTML = JSON.stringify(object);
}
function submitForm(thisObj, thisEvent) {
   var firstName = document.forms["jssond"]["firstName"].value;
   var lastName = document.forms["jssond"]["lastName"].value;
   var email = document.forms["jssond"]["email"].value;
   var mobile = document.forms["jssond"]["mobile"].value;
   var place = document.forms["jssond"]["place"].value;
   var country = document.forms["jssond"]["country"].value;

   var json = {
       firstName: firstName,
       lastName: lastName,
       email: email,
       mobile: mobile,
       place: place,
       country: country
   };
   // Displaying is up to you