【问题标题】:Converting Circular Structure to JSON将循环结构转换为 JSON
【发布时间】:2016-01-11 18:05:47
【问题描述】:

我有两种对象,Beam 和 Sample。样本包含 2 个光束,我有一个样本数组。我需要将数组存储到本地存储中,所以我打电话给localStorage["samples"] = JSON.stringify(samples);,但我收到错误“将循环结构转换为 JSON”。我的对象不包含自身。我还尝试用 1 个 beam 对象替换 samples 对象,但得到相同的错误,并且 Beam 中只有整数和字符串值。

编辑

这是对象。

    function FlexuralStrengthT97(result, method, beam1, beam2, waitForCuring, averageBeams) {
        this.Result = result;
        this.Method = method;
        this.Beam1 = beam1;
        this.Beam2 = beam2;
        this.WaitForCuring = waitForCuring;
        this.AverageOfBeams = averageBeams;

        return this;
    }


    function FSBeam(testingMachineId, beamAge, widthU, widthC, widthL, widthAverage, depthR, depthC, depthL, depthAverage, maxLoad, fs, psi, breakOutside) {
        this.TestingMachineId = testingMachineId;
        this.BeamAge = beamAge;
        this.WidthUpper = widthU;
        this.WidthCenter = widthC;
        this.WidthLower = widthL;
        this.WidthAverage = widthAverage;
        this.DepthRight = depthR;
        this.DepthCenter = depthC;
        this.DepthLeft = depthL;
        this.DepthAverage = depthAverage;
        this.MaxLoad = maxLoad;
        this.FS = fs;
        this.PSI = psi;
        this.BreakOutside = breakOutside;

        return this;
    }

【问题讨论】:

  • 你能提供你的对象吗?
  • 听起来Bearn 有一个属性指向包含它的Sample。这会造成间接循环。
  • @taxicala 我没有任何 JSON,这就是它崩溃的地方。
  • @Grundy 我为我的对象添加了代码。
  • 您是否将这些函数用作构造函数?你用new给他们打电话吗?

标签: javascript arrays object local-storage stringify


【解决方案1】:

这些似乎是构造函数,请确保将它们与 new 关键字一起使用:

var beam1 = new FSBeam();
var flex = new FlexuralStrengthT97();

否则,this 将是 window 而不是实例范围。

【讨论】:

    猜你喜欢
    • 2017-04-03
    • 2018-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-31
    • 2018-10-05
    • 2016-01-22
    相关资源
    最近更新 更多