使用 GetJSON Jquery 解析复杂数组

发布于 2024-08-31 08:48:41 字数 1647 浏览 5 评论 0原文

TLDR:

  1. 这个问题开始,在得到一些信息后简化了它它的工作并在这里继续。
  2. 我需要“获取”JSON 数组并
  3. 正确格式化它,并将数组中的每个数组放置在相应的 DIV 中。
  4. ??
  5. 有用。

这是此问题的后续内容,旨在简化并继续。

我需要从数组中获取一些复杂的 JSON 数据。有了它,我需要标题和它的价值。我这样做的原因是因为我知道数组的名称,但不知道数组内部生成的数据。

可以说这个新数组如下所示:

{
  "Days": [{
      "day": "Sunday",
      "time": "10.00"
    },
    {
      "day": "Monday",
      "time": "12.00"
    },
    {
      "day": "Tuesday",
      "time": "09.00"
    },
    {
      "day": "Wednesday",
      "time": "10.00"
    },
    {
      "day": "Thursday",
      "time": "02.00"
    },
    {
      "day": "Friday",
      "time": "05.00"
    },
    {
      "day": "Saturday",
      "time": "08.00"
    }
  ]
}

感谢(Matthew Flaschen)修复,

我需要它来获得周日和周日。 10:00 以及所有其他人都一样。

我目前解析此代码的代码如下:

$.getJSON(url,
  function(data) {
    $.each(data, function(i, item) {
      $('.testfield').append('<p>' + item + '</p>');
    });
  });

如果没有在每个日期添加时间,它将解析数据如下:

Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday

将时间添加到数组中的日期后,Firebug 不再将其识别为 JSON 字符串。所以我猜我在这里格式化了一些错误的东西。另外,我每天都需要&是时候换一条新线路了,我以为

$('.testfield').append('<p>' + item + '</p>' + '<br />');

会把每一个都应用到新线路上,但这不起作用。

  1. 如何让每一天或每一项目都在新的一行上?
  2. 如何让 $getjson 正确解析日期及其值的数据到 div 中?

TLDR:

  1. Started with this question simplified it after got some of it working and continuing it here.
  2. I need to 'GET' the JSON array
  3. Format it correctly and for each within the array place it in the corresponding DIV.
  4. ??
  5. It works.

This is a followup from this question to simplify and continue.

I need to some complicated JSON data from an array. With it I need the title and it's value. The reason why I am doing this is because I will know what the array is called but not the data that is being generated inside it.

Lets say this new array looks as follows:

{
  "Days": [{
      "day": "Sunday",
      "time": "10.00"
    },
    {
      "day": "Monday",
      "time": "12.00"
    },
    {
      "day": "Tuesday",
      "time": "09.00"
    },
    {
      "day": "Wednesday",
      "time": "10.00"
    },
    {
      "day": "Thursday",
      "time": "02.00"
    },
    {
      "day": "Friday",
      "time": "05.00"
    },
    {
      "day": "Saturday",
      "time": "08.00"
    }
  ]
}

Fixed thanks to (Matthew Flaschen)

I would need it to get Sunday & 10.00 as well as the same for all of the others.

My code to parse this at the moment is as follows:

$.getJSON(url,
  function(data) {
    $.each(data, function(i, item) {
      $('.testfield').append('<p>' + item + '</p>');
    });
  });

Without the added times on each date it will parse the data as follows:

Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday

With the times added to the days in the array Firebug no longer recognizes it as a JSON string. So I am guessing I have formatted something wrong here. Also, I need each day & time to be on a new line I thought that

$('.testfield').append('<p>' + item + '</p>' + '<br />');

Would have applied each one to a new line but that did not work.

  1. How do I get each day or item to be on a new line?
  2. How do I get the $getjson to parse the data correctly day and its value, into a div?

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

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

发布评论

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

评论(3

装纯掩盖桑 2024-09-07 08:48:41

试试这个(重新格式化的 JSON - 下次使用 JSONLint 检查您的 JSON):

{
    "Days": [
        {
            "Sunday": "10.00",
            "Monday": "12.00",
            "Tuesday": "09.00",
            "Wednesday": "10.00",
            "Thursday": "02.00",
            "Friday": "05.00",
            "Saturday": "08.00" 
        }
    ]
}

使用它的脚本:

 $.getJSON( url, function(data){
  $.each(data.Days[0], function(key,value){
   $('.testfield').append('<p>' + key + ' : ' + value + '</p>');
  });
 });

Try this (reformatted JSON - next time check your JSON with JSONLint):

{
    "Days": [
        {
            "Sunday": "10.00",
            "Monday": "12.00",
            "Tuesday": "09.00",
            "Wednesday": "10.00",
            "Thursday": "02.00",
            "Friday": "05.00",
            "Saturday": "08.00" 
        }
    ]
}

Script to work with it:

 $.getJSON( url, function(data){
  $.each(data.Days[0], function(key,value){
   $('.testfield').append('<p>' + key + ' : ' + value + '</p>');
  });
 });
莫多说 2024-09-07 08:48:41

这不是有效的 JSON。执行以下操作:

{"Days":
[{"day": "Sunday", "time": "10.00"},
 {"day": "Monday", "time": "12.00"},
 {"day": "Tuesday", "time": "09.00"},
 {"day": "Wednesday", "time": "10.00"},
 {"day": "Thursday", "time": "02.00"},
 {"day": "Friday", "time": "05.00"},
 {"day": "Saturday", "time": "08.00"}
]}

始终使用 JSONLint 或其他验证器来检查语法。

That's not valid JSON. Do something like:

{"Days":
[{"day": "Sunday", "time": "10.00"},
 {"day": "Monday", "time": "12.00"},
 {"day": "Tuesday", "time": "09.00"},
 {"day": "Wednesday", "time": "10.00"},
 {"day": "Thursday", "time": "02.00"},
 {"day": "Friday", "time": "05.00"},
 {"day": "Saturday", "time": "08.00"}
]}

Always use JSONLint or another validator to check your syntax.

ぃ双果 2024-09-07 08:48:41

有点题外话,但如果你不确定内容,你应该使用安全的方法将数据插入 DOM。否则你的代码存在 XSS 漏洞。

除了安全问题之外,诸如“Foo Bar <[电子邮件受保护]之类的任何数据>”不会按照您(可能)想要的方式呈现。

例如,您可以使用 jQuery text() 方法:

$('.testfield').append($('<p></p>').text(item));

A little off-topic, but if you don't the contents for sure, you should use a safe method to insert data to DOM. Otherwise your code has a XSS vulnerability.

And besides being a security issue, any data such as "Foo Bar <[email protected]>" will not render as you (probably) wanted.

For example, you can use the jQuery text() method:

$('.testfield').append($('<p></p>').text(item));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文