I have this json data in data.json file.
[
"1009 2000",
"1009 2001",
"1002 2002",
"1003 2002"
]
I am looping through this data and spliting the data based on , as in the following code
var show = function () {
var span = $('.input');
var c = $('ul');
$.getJSON('data/data.json', function (id) {
var j = id.split(',');
var hsl = j[0];
var ny = j[1];
$('<li>' + id + '</li>').appendTo(c);
$(c).appendTo(span);
console.log(hsl);
});
}
but this gives me this error in console
Object 1009 2000,1009 2001,1002 2002,1003 2002 has no method 'split'
How can I split this data so that I could display it like this
1009 2000
1009 2001
1002 2002
1003 2002
You don’t need to split anything, simply loop:
jQuery will automatically parse the string returned from the server as a javascript array so that you could directly access its elements.
You could split each element by the space if you want to retrieve the 2 tokens.