简体   繁体   English

JSON.stringify(ComplexArray)返回一个空字符串

[英]JSON.stringify(ComplexArray) returns an empty string

I have the following JavaScript which utilises JQuery and Knockout (I have specific reasons for using the 2 together) 我有以下JavaScript使用JQuery和Knockout(我有具体的理由一起使用2)

$(document).ready(
  function () {
      var Crime = function (Id, CaseNumber, DateOfIncident, Description) {
          var self = this;
          self.Id = Id;
          self.CaseNumber = CaseNumber;
          self.DateOfIncident = DateOfIncident;
          self.Description = Description;
      }

      var CrimesViewModel = function () {
          var self = this;
          //Data
          self.items = ko.observableArray()

          //operations
          addCrime = function () {
              if ($("#AddCrimeForm").valid()) {
                  crime = new Crime(0,
                      $("#AddCrimeForm #CaseNumber").val(),
                      $("#AddCrimeForm #DateOfIncident").val(),
                      $("#AddCrimeForm #Description").val());

                  self.items.push(this.crime);

                  $("#AddCrimeForm #CaseNumber").val("");
                  $("#AddCrimeForm #DateOfIncident").val("");
                  $("#AddCrimeForm #Description").val("");
              }
          }

          self.removeCrime = function (item) {
              self.items.remove(item);
          }

          loadCrimes = function (JSONstring) {
              try {
                  self.JSONItems = JSON.parse(JSONstring);
                  if (self.JSONItems != null)
                      if (self.JSONItems != null)
                          for (i = 0; i < self.JSONItems.length; i++)
                              self.items.push(self.JSONItems[i]);
              }
              catch (e) {
                  alert(e.message + "\n\n" + e.description + "\n\n" + e.stack)
              }
          }

          getCrimes = function () {
              var self = this;
              return JSON.stringify(self.items);
          }

      }

      var CrimesVM = new CrimesViewModel()

      ko.applyBindings(CrimesVM, $("#CrimeList")[0])

  }
);

As illustrated by the above code, I have a ViewModel which basically consists of an array containing Crime objects. 如上面的代码所示,我有一个ViewModel,它基本上由一个包含Crime对象的数组组成。

It is the intent of the getCrimes() function to, when called, serialise the array into JSON. getCrimes()函数的目的是在getCrimes()时将数组序列化为JSON。

The getCrimes() function, unfortunately, returns an empty string, due to the fact that JSON.stringify(self.items) returns an empty string. 遗憾的是, getCrimes()函数返回一个空字符串,因为JSON.stringify(self.items)返回一个空字符串。

Any idea what I am doing wrong here? 知道我在这里做错了什么吗?

** **

UPDATE UPDATE

** **

Here is the working code, based on the answer given below: 这是工作代码,基于下面给出的答案:

$(document).ready(
  function () {
      var Crime = function (Id, CaseNumber, DateOfIncident, Description) {
          var self = this;
          self.Id = Id;
          self.CaseNumber = CaseNumber;
          self.DateOfIncident = DateOfIncident;
          self.Description = Description;
      }

      var CrimesViewModel = function () {
          var self = this;
          //Data
          self.items = ko.observableArray()

          //operations
          addCrime = function () {
              if ($("#AddCrimeForm").valid()) {
                  crime = new Crime(0,
                      $("#AddCrimeForm #CaseNumber").val(),
                      $("#AddCrimeForm #DateOfIncident").val(),
                      $("#AddCrimeForm #Description").val());

                  self.items.push(this.crime);

                  //Update the correspoding hidden field
                  $("#CrimeCollection_New").val(ko.toJSON(self));

                  $("#AddCrimeForm #CaseNumber").val("");
                  $("#AddCrimeForm #DateOfIncident").val("");
                  $("#AddCrimeForm #Description").val("");                  
              }
          }

          self.removeCrime = function (item) {
              self.items.remove(item);
              //Update the correspoding hidden field
              $("#CrimeCollection_New").val(ko.toJSON(self));
          }

          loadCrimes = function (JSONstring) {
              try {
                  self.JSONItems = JSON.parse(JSONstring);
                  if (self.JSONItems != null)
                      if (self.JSONItems != null)
                          for (i = 0; i < self.JSONItems.length; i++)
                              self.items.push(self.JSONItems[i]);
              }
              catch (e) {
                  alert(e.message + "\n\n" + e.description + "\n\n" + e.stack)
              }
          }



      }

      var CrimesVM = new CrimesViewModel()

      ko.applyBindings(CrimesVM, $("#CrimeList")[0])

  }
);

Because self.items is a Knockout observable, you should use the Knockout function .toJS() to get the values: ko.toJS(self.items) . 因为self.items是一个Knockout可观察对象,所以你应该使用Knockout函数.toJS()来获取值: ko.toJS(self.items)

Or, if you want to convert the items directly to JSON, you can use .toJSON() . 或者,如果要将项目直接转换为JSON,则可以使用.toJSON()

Knockout: Loading and saving JSON data 淘汰赛:加载和保存JSON数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM