使用 GetJSON Jquery 解析复杂数组
TLDR:
- 从这个问题开始,在得到一些信息后简化了它它的工作并在这里继续。
- 我需要“获取”JSON 数组并
- 正确格式化它,并将数组中的每个数组放置在相应的 DIV 中。
- ??
- 有用。
这是此问题的后续内容,旨在简化并继续。
我需要从数组中获取一些复杂的 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 />');
会把每一个都应用到新线路上,但这不起作用。
- 如何让每一天或每一项目都在新的一行上?
- 如何让 $getjson 正确解析日期及其值的数据到 div 中?
TLDR:
- Started with this question simplified it after got some of it working and continuing it here.
- I need to 'GET' the JSON array
- Format it correctly and for each within the array place it in the corresponding DIV.
- ??
- 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.
- How do I get each day or item to be on a new line?
- How do I get the $getjson to parse the data correctly day and its value, into a div?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个(重新格式化的 JSON - 下次使用 JSONLint 检查您的 JSON):
使用它的脚本:
Try this (reformatted JSON - next time check your JSON with JSONLint):
Script to work with it:
这不是有效的 JSON。执行以下操作:
始终使用 JSONLint 或其他验证器来检查语法。
That's not valid JSON. Do something like:
Always use JSONLint or another validator to check your syntax.
有点题外话,但如果你不确定内容,你应该使用安全的方法将数据插入 DOM。否则你的代码存在 XSS 漏洞。
除了安全问题之外,诸如“Foo Bar <[电子邮件受保护]之类的任何数据>”不会按照您(可能)想要的方式呈现。
例如,您可以使用 jQuery text() 方法:
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: