Javascript中JSON字符串化后的JSON格式错误
我有一个Javascript中JSON字符串化后的JSON格式错误,javascript,json,Javascript,Json,我有一个div,其中存储了我在以下Javascript函数中创建的表单中的字段: information: function(){ var objData = $("#InformationJson").html(); var dataJSON = JSON.parse(objData); $("#informationDialog").dialog({ autoOpen: false,
div
,其中存储了我在以下Javascript函数中创建的表单中的字段:
information: function(){
var objData = $("#InformationJson").html();
var dataJSON = JSON.parse(objData);
$("#informationDialog").dialog({
autoOpen: false,
width: 520, height: 470,
close: function() {
$("#informationForm").validate().resetForm();
$("#informationForm").find(".error").removeClass("error");
}
});
$.validator.addMethod("regx", function(value, element, regexpr) {
return regexpr.test(value);
}, "");
$("#informationForm").validate({
rules: {
name: {
required: true,
regx: /^([Α-Ωα-ωa-zA-Z0-9_]+)$/
},
displayed_name: { required: true},
mode: { required: true},
process_group: { required: false},
process_admin: { required: false},
process_in_force_from: { required: false},
process_in_force_to: { required: false},
created: { required: false},
website: { required: false},
version: {
required: true,
regx: /^-?\d\d*$/
},
description: { required: false},
},
messages: {
name: "Field should include characters and/or underscore(s)",
displayed_name: "",
mode: "",
process_group: "",
process_admin: "Please select the Process Admin",
process_in_force_from: "Please fill in the day from which the Process will be in force",
process_in_force_to: "Please fill in the day to which the Process will be in force",
website: "Please fill in the website",
created: "Please fill in the date at which the Process was created ",
version: "Field should include non-negative integers",
description: "Please fill in the Process Description",
},
submitHandler: function() {
formSubmitHandler();
}
});
var formSubmitHandler = $.noop;
var showInformationDialog = function(client) {
if(client){
$("#name").val(client.name);
$("#displayed_name").val(client.displayed_name);
$("#mode").val(client.mode);
$("#process_group").val(client.process_group);
$("#process_admin").val(client.process_admin);
$("#process_in_force_from").val(client.process_in_force_from);
$("#process_in_force_to").val(client.process_in_force_to);
$("#website").val(client.website);
$("#created").val(client.created);
$("#version").val(client.version);
$("#description").val(client.description);
}
else{
$("#name").val('');
$("#displayed_name").val('');
$("#mode").val('');
$("#process_group").val('');
$("#process_admin").val('');
$("#process_in_force_from").val('');
$("#process_in_force_to").val('');
$("#website").val('');
$("#created").val('');
$("#version").val(["1"]);
$("#description").val('');
}
formSubmitHandler = function() {
saveClient(client);
};
$("#informationDialog").dialog("option", "title", "Process")
.dialog("open");
};
var saveClient = function(client) {
$.extend(client, {
name: $("#name").val(),
displayed_name: $("#displayed_name").val(),
mode: parseInt($("#mode").val(), 10),
process_group: parseInt($("#process_group").val(), 10),
process_admin: parseInt($("#process_admin").val(), 10),
process_in_force_from: $("#process_in_force_from").val(),
process_in_force_to: $("#process_in_force_to").val(),
website: $("#website").val(),
created: $("#created").val(),
version: $("#version").val(),
description: $("#description").val(),
});
var myhtml = {
"name": $("#name").val(),
"displayed_name": $("#displayed_name").val(),
"mode": $("#mode").val(),
"process_group": $("#process_group").val(),
"process_admin": $("#process_admin").val(),
"process_in_force_from": $("#process_in_force_from").val(),
"process_in_force_to": $("#process_in_force_to").val(),
"created": $("#created").val(),
"website": $("#website").val(),
"version": $("#version").val(),
"description": $("#description").val() };
$("#InformationJson").html(JSON.stringify(myhtml));
$("#informationDialog").dialog("close");
};
showInformationDialog(dataJSON);
},
我用以下代码将它们保存为json字符串:
var InformationJson = document.getElementById('InformationJson').textContent;
var jsonString = JSON.stringify(InformationJson);
但我得到的JSON字符串如下所示:
"{\"name\":\"test\",\"displayed_name\":\"test\",\"mode\":\"1\",\"process_group\":\"2\",\"process_admin\":\"2\",\"process_in_force_from\":\"2017-01-05\",\"process_in_force_to\":\"2017-01-19\",\"created\":\"2017-01-26\",\"website\":\"test\",\"version\":\"3\",\"description\":\"test\"}"
如何将其作为常规JSON对象获取,即:
{"name":"test","displayed_name":"test","mode": 1 ,....,"description":"test"}
我错在哪里?变量jsonString
包含表示javascript对象的字符串。这是字符串化对象时得到的格式。您要获取的格式是一个对象,您可以这样获取它:
var object = JSON.parse(jsonString);
检查以下示例
var jsonString=“{\\\\\\\”:“测试\”,“显示的名称\”:“测试\”,“模式\”:“1\”,“进程组\”:“2\”,“进程管理\”:“2\”,“进程强制从\\”:“2017-01-05\”,“进程强制到\\”:“2017-01-19\”,“已创建\\”:“2017-01-26\”,“测试网站”,“版本描述\\”,“测试说明”;
$('#getObject')。单击(函数(){
var obj=JSON.parse(jsonString);
控制台日志(obj);
})
检查控制台的输出
Get Object
变量jsonString
包含一个表示javascript对象的字符串。这是字符串化对象时得到的格式。您要获取的格式是一个对象,您可以这样获取它:
var object = JSON.parse(jsonString);
检查以下示例
var jsonString=“{\\\\\\\”:“测试\”,“显示的名称\”:“测试\”,“模式\”:“1\”,“进程组\”:“2\”,“进程管理\”:“2\”,“进程强制从\\”:“2017-01-05\”,“进程强制到\\”:“2017-01-19\”,“已创建\\”:“2017-01-26\”,“测试网站”,“版本描述\\”,“测试说明”;
$('#getObject')。单击(函数(){
var obj=JSON.parse(jsonString);
控制台日志(obj);
})
检查控制台的输出
Get Object
当您使用.textContent
时,您只是从div中获取文本,而不是期望的JSON。您可以使用JSON.parse(jsonString)
在导出文本时将数据作为对象获取。当您使用.textContent
时,您只是从div中获取文本,而不是期望的JSON。您可以使用JSON.parse(jsonString)
在导出文本时将数据作为对象获取。这是您获取的转义JSON字符串-这是正确的,也是JSON.stringify
所做的。
要取回JSON对象,可以使用JSON.parse:
JSON.parse(jsonString)
这是您得到的转义JSON字符串-这是正确的,JSON.stringify
所做的。
要取回JSON对象,可以使用JSON.parse:
JSON.parse(jsonString)