如何通过Javascript访问字符串化的JSON变量

时间:2019-04-02 22:46:30

标签: javascript json

我想做的是访问存储在字符串化JSON文件中的变量。我正在使用P5.js库对文件进行字符串化。我遇到了变量未定义的问题。

在这里下载文件:

function saveBird() {
  let bird = birds[0];
  saveJSON(bird.brain, "bird.json");
}

在这里我加载文件:

function loadBird() {
  loading = true;

  brainJSON = loadJSON(prompt("What is the name of the JSON file?") + ".JSON");
  cycles = slider.value();
  pFrames = 120 / cycles;
  frameCount = 0;
  pipes.splice(0, Infinity);
  birds.splice(0, Infinity);
  savedBirds.splice(0, Infinity);
  gen = 0;
  saveSetup();
}

//--------------------------------------------------------------------------

function saveSetup() {
  var birdBain = NeuralNetwork.deserialize(brainJSON);
  for (i = 0; i < total; i++) {
    bird = new Bird(birdBrain);
  }
  loading = false;
}

static deserialize(data) {
    if (typeof data == 'string') {
      data = JSON.parse(data);
    }
    let matrix = new Matrix(data.rows, data.cols);
    matrix.data = data.data;
    return matrix;
  }

  static deserialize(data) {
    if (typeof data == 'string') {
      data = JSON.parse(data);
    }
    let nn = new NeuralNetwork(data.input_nodes, data.hidden_nodes, data.output_nodes);
    nn.weights_ih = Matrix.deserialize(data.weights_ih);
    nn.weights_ho = Matrix.deserialize(data.weights_ho);
    nn.bias_h = Matrix.deserialize(data.bias_h);
    nn.bias_o = Matrix.deserialize(data.bias_o);
    nn.learning_rate = data.learning_rate;
    return nn;
  }

0 个答案:

没有答案