Reputation: 105
I have a javascript/jQuery script that takes json objects and then does something with them in a table
Here is the relevant bit of the JS/jQuery
$.getJSON("People.json",
function(data) {
content = '<tr>';
content += '<tbody>';
$.each(data.People, function(i, PersonObj) {
var Person = PersonObj[Object.keys(PersonObj)[0]];
this snippet was taking JSON objects from the follwing format,
{
"People": [
{
"Person1": {
"Op": "5709",
"Name": "Persons name",
"WorkHours": 5,
"Start": " 3:00PM",
"End": " 8:00PM",
"Clock": false,
"OFF": false,
"ON": false,
"OUT": false,
"Late": 23484360,
"Home": 23484660,
"HomeEarly": 23484660,
"DifHours": 0,
"Shift": "254"
}
},
{
"Person2": {
"Op": "5703",
"Name": "Persons name",
"WorkHours": 12.25,
"Start": " 7:45PM",
"End": " 8:00AM",
"Clock": false,
"OFF": false,
"ON": false,
"OUT": false,
"Late": 23484645,
"Home": 23483940,
"HomeEarly": 23483940,
"DifHours": 0,
"Shift": "251"
}
}
Here is the new format
[
{
"Op": "5709",
"Name": "Persons name",
"WorkHours": 5,
"Start": " 3:00PM",
"End": " 8:00PM",
"Clock": false,
"OFF": false,
"ON": false,
"OUT": false,
"Late": 23484360,
"Home": 23484660,
"HomeEarly": 23484660,
"DifHours": 0,
"Shift": "254"
},
{
"Op": "5703",
"Name": "Persons name",
"WorkHours": 12.25,
"Start": " 7:45PM",
"End": " 8:00AM",
"Clock": false,
"OFF": false,
"ON": false,
"OUT": false,
"Late": 23484645,
"Home": 23483940,
"HomeEarly": 23483940,
"DifHours": 0,
"Shift": "251"
}
how can i get the JS/JQuery to work w
Upvotes: 0
Views: 66
Reputation: 63524
I would write a simple conversion function that takes your existing data and returns a new clean set:
function convert(data) {
var people = data.People;
var out = [];
for (var i = 0, l = people.length; i < l; i++) {
var person = people[i];
var key = Object.keys(person);
out.push(person[key]);
}
return out;
}
Upvotes: 0
Reputation: 705
I suppose you could simply handle data as a field, since there are no other declarations on top of the JSON:
$.getJSON("People.json",
function(data) {
content = '<tr>';
content += '<tbody>';
for(var i = 0; i < data.length; i++) {
var person = data[i];
var op = person.Op;
var name = person.Name;
// etc...
}
...
Upvotes: 1