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?