在Javascript中使用Javascript进行字符串化后,JSON格式错误

时间:2017-01-24 14:22:58

标签: javascript json

我有一个div,其中我存储了我在以下Javascript函数中创建的form字段:

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"}

我哪里错了?

3 个答案:

答案 0 :(得分:0)

jsonString变量包含表示javascript对象的字符串。这是您对字符串化时获得的格式。你想要获得的格式是一个对象,你可以这样得到它:

var object = JSON.parse(jsonString);

检查以下示例



var jsonString = "{\"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\"}";

$('#getObject').click(function() {
  var obj = JSON.parse(jsonString);
  console.log(obj);
})

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<p>Check the console for the output!!!</p>
<button id="getObject" type="button">Get Object</button>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

使用.textContent时,您只是从div中获取文本,而不是您期望的JSON。您可以使用JSON.parse(jsonString)将您的数据作为对象提取。

答案 2 :(得分:0)

这是您获得的转义JSON字符串 - 这是正确的,JSON.stringify是什么。 要获取JSON对象,可以使用JSON.parse:

JSON.parse(jsonString)