簡體   English   中英

json.stringify沒有在主對象下的所有對象上調用toJson

[英]json.stringify is not calling toJson on all objects under main object

在IE 11中,在我的對象上調用Stringify不會遞歸在樹中的所有對象上調用toJson。 我有一個自定義的toJson函數

Object.prototype.toJSON = function() {
  if (!(this.constructor.name == "Array")) {
    if (JSON._serialized[this._id]) {
      if (this.material) {
        return {
          name: this.name,
          material: this.material
        };
      } else {
        return { referenceId: this._id };
      }
    }
    var json = this.toJSONProperties();
    json._id = this._id;
    json._runID = this._runID;
    json._className = this.constructor.name;
    JSON._serialized[this._id] = true;
    return json;
  } else {
    return this;
  }
}

這適用於chrome,firefox和safari,但在IE 11中,當我調用var json = JSON.stringify(object);時,此toJson函數僅在頂級對象上被調用var json = JSON.stringify(object); 我正在使用的想法是必須toJson是所有類上的通用函數,然后使用var json = this.toJSONProperties(); 在我要轉換的特定類上。 任何想法為什么這只會在IE中失敗?

更新

我解決了這個問題。 事實證明this.constructor.name在IE中不起作用。 將其更改為this.constructor.toString().match(/function (.{1,})\\(/)[1]可修復錯誤。

this.constructor.name在IE中不起作用。 將其更改為this.constructor.toString().match(/function (.{1,})\\(/)[1]可解決此問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM