JSON数据成变量

发布于 2025-01-30 17:09:20 字数 1080 浏览 4 评论 0原文

edit :我解决了该错误,但是,它似乎仅打印出一个实例。我该如何使所有条目都进入该变量?

我对此有些新,但我正在尝试。抱歉,这个问题听起来很愚蠢。 我配置了JSON服务器,它可以正常工作。我可以成功地删除数据,但是,我收到的数据要进入一个变量,以便以后可以在另一个函数中使用它。为此,我创建了变量日期图。但是,它似乎不好。它似乎没有在每个循环之外读取它。有什么想法是什么? 我尝试在每个循环中使用console.log(),并且效果很好,但是它在它之外行不通。

function solicitare() {
  adresa = "http://localhost:4000/listaexamene?callback=?";
  $.getJSON(adresa, function (raspuns) {
    $.each(raspuns, function (indice, examen) {
      continutDeAfisat = "<div><i>Subiect: " + examen.examId + "</i>, Student: " + examen.studentId + "</div>";
      $(continutDeAfisat).appendTo("#datenoi");

      var dategraph = {};
      dategraph.examId = examen.examId;
      dategraph.studentId = examen.studentId;
    });
    console.log(dategraph);
    stringNou = JSON.stringify(dategraph);
    console.log(stringNou);
  });
}

我收到的错误如下:

”在此处输入图像说明”

EDIT: I solved that error, however, it only seems to print out just one instance. How can I make it put all the entries into that variable?

I'm a little bit too new to this, but I'm trying. Sorry if the question sounds stupid.
I've configured a JSON server and it works just fine. I can pull out the data successfully, however, the data I receive I'd like to go into a variable so that I can use it later on in another function. For this purpose, I created the variable dategraph. However, it doesn't seem to work good. It doesn't seem to read it outside the $each loop. Any ideas what could it be?
I tried using console.log() in the $each loop and it works fine, however, it won't work outside of it.

function solicitare() {
  adresa = "http://localhost:4000/listaexamene?callback=?";
  $.getJSON(adresa, function (raspuns) {
    $.each(raspuns, function (indice, examen) {
      continutDeAfisat = "<div><i>Subiect: " + examen.examId + "</i>, Student: " + examen.studentId + "</div>";
      $(continutDeAfisat).appendTo("#datenoi");

      var dategraph = {};
      dategraph.examId = examen.examId;
      dategraph.studentId = examen.studentId;
    });
    console.log(dategraph);
    stringNou = JSON.stringify(dategraph);
    console.log(stringNou);
  });
}

The error I receive is the following:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阳光①夏 2025-02-06 17:09:20

您正在声明内部范围中的变量。因此,外部范围不知道该变量是什么。尝试:

function solicitare() {
  const dategraph = {}; // <---
  const adresa = "http://localhost:4000/listaexamene?callback=?";
  $.getJSON(adresa, function (raspuns) {
    $.each(raspuns, function (indice, examen) {
      const continutDeAfisat = "<div><i>Subiect: " + examen.examId + "</i>, Student: " + examen.studentId + "</div>";
      $(continutDeAfisat).appendTo("#datenoi");

      dategraph.examId = examen.examId;
      dategraph.studentId = examen.studentId;
    });
    console.log(dategraph);
    const stringNou = JSON.stringify(dategraph);
    console.log(stringNou);
  });
}

使用代替。

避免使用var,然后 不会对你很满意。声明新变量时,请始终使用const

使用const当您 wont 将值重新分配给变量时。

使用Let将来将 will 将值重新分配给一个变量。


本地箭头功能。而不是这样做:

$.getJSON(adresa, function (raspuns) {
  $.each(raspuns, function (indice, examen) {

您可以这样做:

$.getJSON(adresa, (raspuns) => {
  $.each(raspuns, (indice, examen) => {

通常不想这样做的唯一一次是在JavaScript中使用关键字时。


未使用的本地变量 IE Indice在您的情况下,由于您不使用它。人们通常会_表明它是未使用的变量。因此,您可以使用do _indice_,甚至更容易,只需do (,examen)


字符串插值。如您所见,做“&lt; div&gt;&lt; i&gt;主题非常烦人:而不是“”``制作字符串。然后,您可以使用类似$ {evign.examid}的变量进行字符串插值。


这就是我要做的。

function solicitare() {
  const dategraph = {};
  const adresa = "http://localhost:4000/listaexamene?callback=?";
  $.getJSON(adresa, (raspuns) => {
    $.each(raspuns, (_, examen) => {
      const continutDeAfisat = `<div><i>Subiect: ${examen.examId}</i>, Student: ${examen.studentId}</div>`;
      $(continutDeAfisat).appendTo("#datenoi");

      dategraph.examId = examen.examId;
      dategraph.studentId = examen.studentId;
    });
    console.log(dategraph);
    const stringNou = JSON.stringify(dategraph);
    console.log(stringNou);
  });
}

如果您在VS代码中编写JavaScript,我还可以建议安装一个称为Prettier的扩展程序,该扩展名将有助于格式化您的代码并使其更可读。


新问题我如何使它从循环中保存所有答案,而不仅仅是一个?

尝试以下操作:

首先,制作dategraph阵列。然后,我们将每个结果对象推入数组。这样的事情:

function solicitare() {
  const dategraph = []; // <--- make array
  const adresa = "http://localhost:4000/listaexamene?callback=?";
  $.getJSON(adresa, (raspuns) => {
    $.each(raspuns, (_, examen) => {
      const continutDeAfisat = `<div><i>Subiect: ${examen.examId}</i>, Student: ${examen.studentId}</div>`;
      $(continutDeAfisat).appendTo("#datenoi");
      
      // push result into array
      dategraph.push({
        examId: examen.examId,
        studentId: examen.studentId,
      });
    });
    console.log(dategraph);
    const stringNou = JSON.stringify(dategraph);
    console.log(stringNou);
  });

  console.log("Final dategraph:", JSON.stringify(dategraph, null, 2));
}

You're declaring the variable in an inner scope. So the outer scopes don't know what that variable is. Try:

function solicitare() {
  const dategraph = {}; // <---
  const adresa = "http://localhost:4000/listaexamene?callback=?";
  $.getJSON(adresa, function (raspuns) {
    $.each(raspuns, function (indice, examen) {
      const continutDeAfisat = "<div><i>Subiect: " + examen.examId + "</i>, Student: " + examen.studentId + "</div>";
      $(continutDeAfisat).appendTo("#datenoi");

      dategraph.examId = examen.examId;
      dategraph.studentId = examen.studentId;
    });
    console.log(dategraph);
    const stringNou = JSON.stringify(dategraph);
    console.log(stringNou);
  });
}

Avoid using var and use let instead.

Simply doing myNewVariable = {}; will work, but javascript will not be very happy with you. When declaring new variables always use let or const.

Use const when you wont re-assign a value to a variable.

Use let when you will re-assign a value to a variable in the future.


Local Arrow Functions. Instead of doing:

$.getJSON(adresa, function (raspuns) {
  $.each(raspuns, function (indice, examen) {

You can do:

$.getJSON(adresa, (raspuns) => {
  $.each(raspuns, (indice, examen) => {

The only time you generally don't want to do this, is when you are working with the this keyword in javascript.


Unused local variables i.e. indice in your case, since you aren't using it. People usually do _ to indicate that it's an unused variable. So you can use either do _indice or _, or even easier, just do (, examen).


String interpolation. As you can see, its pretty annoying to do "<div><i>Subject:" + examen.examId + "....". Instead of "" make the string with ``. Then you can do string interpolation with variables like this ${examen.examId}.


This is what I'd do.

function solicitare() {
  const dategraph = {};
  const adresa = "http://localhost:4000/listaexamene?callback=?";
  $.getJSON(adresa, (raspuns) => {
    $.each(raspuns, (_, examen) => {
      const continutDeAfisat = `<div><i>Subiect: ${examen.examId}</i>, Student: ${examen.studentId}</div>`;
      $(continutDeAfisat).appendTo("#datenoi");

      dategraph.examId = examen.examId;
      dategraph.studentId = examen.studentId;
    });
    console.log(dategraph);
    const stringNou = JSON.stringify(dategraph);
    console.log(stringNou);
  });
}

If you write your javascript in VS code, I can also recommend to install an extension called prettier which will help format your code and make it more readable.


New question: How can I make it that it saves all the answers from the loop and not just one?

Try this:

First, make dategraph an array. Then we push each result object into the array. So something like this:

function solicitare() {
  const dategraph = []; // <--- make array
  const adresa = "http://localhost:4000/listaexamene?callback=?";
  $.getJSON(adresa, (raspuns) => {
    $.each(raspuns, (_, examen) => {
      const continutDeAfisat = `<div><i>Subiect: ${examen.examId}</i>, Student: ${examen.studentId}</div>`;
      $(continutDeAfisat).appendTo("#datenoi");
      
      // push result into array
      dategraph.push({
        examId: examen.examId,
        studentId: examen.studentId,
      });
    });
    console.log(dategraph);
    const stringNou = JSON.stringify(dategraph);
    console.log(stringNou);
  });

  console.log("Final dategraph:", JSON.stringify(dategraph, null, 2));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文