Just change the following line
otArr.push('"' + e + '": {' + itArr.join(',') + '}');
to
otArr.push('"' + (e+1) + '": {' + itArr.join(',') + '}');
The parenthesis will add the values as numbers not strings.
Also, add keys array for internal object keys.
function html2json() {
var json = '{';
var otArr = [];
// var i = 1;
var tbl2 = $('table tbody tr').each(function(e) {
x = $(this).children();
var itArr = [];
var keys = ['no','name','lastname'];
x.each(function(i) {
itArr.push('"' + keys[i] + '":"' + $(this).text() + '"');
});
otArr.push('"' + (e+1) + '": {' + itArr.join(',') + '}');
})
json += otArr.join(",") + '}'
return json;
}
Answer from mhatch on Stack OverflowJust change the following line
otArr.push('"' + e + '": {' + itArr.join(',') + '}');
to
otArr.push('"' + (e+1) + '": {' + itArr.join(',') + '}');
The parenthesis will add the values as numbers not strings.
Also, add keys array for internal object keys.
function html2json() {
var json = '{';
var otArr = [];
// var i = 1;
var tbl2 = $('table tbody tr').each(function(e) {
x = $(this).children();
var itArr = [];
var keys = ['no','name','lastname'];
x.each(function(i) {
itArr.push('"' + keys[i] + '":"' + $(this).text() + '"');
});
otArr.push('"' + (e+1) + '": {' + itArr.join(',') + '}');
})
json += otArr.join(",") + '}'
return json;
}
You can convert e to a Number and add one to it like in this fiddle.
function html2json() {
var json = '{';
var otArr = [];
// var i = 1;
var tbl2 = $('table tbody tr').each(function(e) {
x = $(this).children();
var itArr = [];
x.each(function() {
itArr.push('"' + $(this).text() + '"');
});
otArr.push('"' + (Number(e) + 1) + '": {' + itArr.join(',') + '}');
})
json += otArr.join(",") + '}'
return json;
}
The json you're returning is not valid though. You may want to do something like this fiddle if you can to simplify and ensure valid json and to create your objects from any table structure.
function html2json() {
var otArr = [];
var tblHeaders = Array.from($('table thead tr')
.children())
.map(header => $(header).text());
var tbl2 = $('table tbody tr').each(function(e) {
const values = Array.from($(this).children());
const row = {};
for (let i = 0; i < tblHeaders.length; i++){
row[tblHeaders[i]] = $(values[i]).text();
}
otArr.push({
[e+1]: row
})
})
json = JSON.stringify(otArr);
return json;
}
Profile both. Optimize afterwards.
Synthesizing other answers:
- Your wire format doesn't have to be the same as your in-memory format.
- Profile which is better - see if it makes a difference.
- Simpler is usually better to start with.
Further:
- If you just have a page of results, and few users, then the 2nd format may be no worse than the 1st format.
- If your data is quite sparse, the 2nd format may well be better.
- If you're sending 1000's or rows of data, and you have millions of users, then it's possible that the size of data you send can start to matter, and perhaps the 1st format may help.
- You can't guarantee that all user agents support gzip / deflate, so bear this in mind.
I know 3 JSON specifications : JSON-LD, HAL and JSON API. I prefer the second.
So I will write your json like this :
{
"buckets": [
{
"id": 1,
"orange": 3,
"apple": 4,
"banana": 7
},
{
"id": 2,
"orange": 5,
"apple": 6,
"banana": 2
},
{
"id": 3,
"orange": 5,
"apple": 11,
"banana": 8
}
]
}
@Andy's response is more like JSON API specification.
Think about how you want to use your JSON. Your suggested answer breaks up the contents of one bucket across lots of objects. To find the number of oranges in bucket 1, you would have to write a loop. Yuck. I would suggest...
"buckets" : {
"1":{"oranges":3,"apples":4,"bananas":7},
"2":{"oranges":5,"apples":6,"bananas":2},
"3":{"oranges":5,"apples":11,"bananas":8}
}
If you have an ID, use it as the name of the bucket objects. This lets you access the objects by ID afterwards. For example
buckets["1"].oranges