Answer the question
In order to leave comments, you need to log in
How to upload data from JSON to local html?
Good evening.
There is a playback.json json file with the following structure:
{
"playing": true,
"song": {
"title": "Can't Get You out of My Head",
"artist": "Jack Lukeman",
"album": "Sunday Independent",
"albumArt": "https://lh3.googleusercontent.com/q8vGPE-zyi0Kr6XzhHijz3-Nu0bPflB2wfSSJIfyIIxAYr8rouf8PWnni_LFMrp3tmHu1sCMVA"
},
"rating": {
"liked": false,
"disliked": false
},
"time": {
"current": 101576,
"total": 185000
},
"songLyrics": null,
"shuffle": "NO_SHUFFLE",
"repeat": "NO_REPEAT"
}
$(function(){
$.getJSON('playback.json', function(data) {
for(var i=0;i<data.song.length;i++){
$('#song').append('<tr><td>' + data.song[i].title);
}
});
});
Answer the question
In order to leave comments, you need to log in
Maybe your original json is being given as a string in html and not as a json object.
Try this:
$.getJSON('playback.json', function(data) {
data = JSON.parse(data);
...
if it becomes normal, then the problem is this
Working with objects is somewhat different from working with arrays ( array-like objects )
$(function() {
$.getJSON('playback.json', function(data) {
for (var key in data.song) {
if (data.song.hasOwnProperty(key)) {
$('#song').append('<tr><td>' + data.song[key]);
}
}
});
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question