I have JSON content that I want to convert to an array. That JSON content is a HTML table with cell values. Those cells I want to convert into one single array, so I can use that further in my project.
I'm struggeling to convert those cell values into an array.
So what I have is this:
JSON (very small snippet):
{
"textpage": {
"content": "<table width=\"512\">\r\n<tbody>\r\n<tr>\r\n<td width=\"64\">8211</td>\r\n<td width=\"64\">8231</td>\r\n<td width=\"64\">1309</td>\r\n<td width=\"64\">1333</td>\r\n<td width=\"64\">1011</td>\r\n<td width=\"64\">1035</td>\r\n<td width=\"64\">1062</td>\r\n<td width=\"64\">1087</td>\r\n</tr>\r\n<tr>\r\n<td>8212</td>\r\n<td>8232</td>\r\n<td>1311</td>\r\n<td>1334</td>\r\n<td>1012</td>\r\n<td>1036</td>\r\n<td>1063</td>\r\n<td>1091</td>\r\n</tr>\r\n<tr>\r\n<td>8218</td>\r\n<td>8233</td>\r\n<td>1312</td>\r\n<td>1335</td>\r\n<td>1013</td>\r\n<td>1037</td>\r\n<td>1064</td>\r\n<td>1092</td>\r\n</tr>\r\n<tr>\r\n<td>8219</td>\r\n<td>8239</td>\r\n<td>1313</td>\r\n<td>1336</td>\r\n<td>1014</td>
///////// and whole lot more stuff //////////
},
jQuery:
function getPostalcode(){
var url = 'link-to-text-page';
$.getJSON(url+'?format=json', function(data){
var content = data.textpage.content,
codes = [];
$(content + 'tr td').each(function(){
var code = $(this).text();
codes.push(code)
});
codes.join('');
console.log(codes)
});
}
That console.log returns this:
Array [ " 8211 8231 1309 1333 1011 1035 10…" ]
I need it to read like
Array ["8211,8231,1309,1333,1011,1035,10…" ]
The problem is that those cell values are text. So how can I convert those values in variables/array?
I tried things like:
var code = $(this).text();
parsed = JSON.parse(code)
Or:
var code = $(this).text();
$(code).text(function(i, val) {
return val.replace(/,/g, ", ");
});
Any help greatly appreciated!