json naar array
Ik heb volgende json terug van een pagina
Code (php)
1
{"Ok":true,"aantalDB":8,"aantalfiles":8,"data":[{"lied":"Hallo Hallo","performer":"Corrie Van Gorp","sn":"DNCD 3333","d":"1","n":"1","titel":"Dino's Superfeest CD"},{"lied":"Polonaise Hollandaise (Polon\u00e4se Blankene","performer":"Arie Ribbens","sn":"DNCD 3333","d":"1","n":"2","titel":"Dino's Superfeest CD"},{"lied":"'S Nachts Na Twee\u00ebn","performer":"De Havenzangers","sn":"DNCD 3333","d":"1","n":"3","titel":"Dino's Superfeest CD"},{"lied":"Bij Ons Staat Op De Keukendeur","performer":"De Twee Pinten","sn":"DNCD 3333","d":"1","n":"4","titel":"Dino's Superfeest CD"},{"lied":"'K Heb Hele Grote Bloemkoole","performer":"Andr\u00e9 Van Duin","sn":"DNCD 3333","d":"1","n":"5","titel":"Dino's Superfeest CD"},{"lied":"Sophia Loren","performer":"Jack De Nijs","sn":"DNCD 3333","d":"1","n":"6","titel":"Dino's Superfeest CD"},{"lied":"Ammenooitnie","performer":"Gebroeders Grimm","sn":"DNCD 3333","d":"1","n":"7","titel":"Dino's Superfeest CD"},{"lied":"Het Leven Is Goed In M'N Brabantse Land","performer":"Oh Sixteen Oh Seven","sn":"DNCD 3333","d":"1","n":"8","titel":"Dino's Superfeest CD"}]}
de json staat in variable js.
Nu wil ik door de gegevens lopen maar dat lukt me niet.
Code (php)
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
let songs = JSON.parse(js);
let lijst = document.getElementById("lijst");
if(songs.Ok) {//ook geprobeerd met songs['Ok']
for(i=0;i<songs.aantalfiles;i++) {
let li = document.createElement("li");
li.appendChild(document.createTextNode(songs[i]['lied']) );
lijst.appendChild(li);
}
}
let lijst = document.getElementById("lijst");
if(songs.Ok) {//ook geprobeerd met songs['Ok']
for(i=0;i<songs.aantalfiles;i++) {
let li = document.createElement("li");
li.appendChild(document.createTextNode(songs[i]['lied']) );
lijst.appendChild(li);
}
}
Wat doe ik fout?
Jan
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<ul id="lijst">
</ul>
<script>
var lijst = document.getElementById("lijst");
if( songs.Ok )
{
for (var i = 0; i < songs.data.length; i++)
{
song = songs.data[i];
var node = document.createElement("li");
var textnode = document.createTextNode(song.lied + " (" + song.performer + ")");
node.appendChild(textnode);
lijst.appendChild(node);
}
}
</script>
</ul>
<script>
var lijst = document.getElementById("lijst");
if( songs.Ok )
{
for (var i = 0; i < songs.data.length; i++)
{
song = songs.data[i];
var node = document.createElement("li");
var textnode = document.createTextNode(song.lied + " (" + song.performer + ")");
node.appendChild(textnode);
lijst.appendChild(node);
}
}
</script>
Song is ok maar Ok, data en de rest niet
Geen foutmelding in de console? Doe anders gewoon even een console.log(songs).
Ik maak de string in php met deze code:
echo json_encode(array('Ok'=>true, 'aantalDB'=>mysqli_num_rows($result), 'aantalfiles'=>$aantal, 'data'=>$a));
$a is de info.
Ik vermoed problemen met de enkele aanhalingstekens.
Jan
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<script>
js = '{"Ok":true,"aantalDB":8,"aantalfiles":8,"data":[{"lied":"Hallo Hallo","performer":"Corrie Van Gorp","sn":"DNCD 3333","d":"1","n":"1","titel":"Dino\'s Superfeest CD"},{"lied":"Polonaise Hollandaise (Polon\u00e4se Blankene","performer":"Arie Ribbens","sn":"DNCD 3333","d":"1","n":"2","titel":"Dino\'s Superfeest CD"},{"lied":"\'S Nachts Na Twee\u00ebn","performer":"De Havenzangers","sn":"DNCD 3333","d":"1","n":"3","titel":"Dino\'s Superfeest CD"},{"lied":"Bij Ons Staat Op De Keukendeur","performer":"De Twee Pinten","sn":"DNCD 3333","d":"1","n":"4","titel":"Dino\'s Superfeest CD"},{"lied":"\'K Heb Hele Grote Bloemkoole","performer":"Andr\u00e9 Van Duin","sn":"DNCD 3333","d":"1","n":"5","titel":"Dino\'s Superfeest CD"},{"lied":"Sophia Loren","performer":"Jack De Nijs","sn":"DNCD 3333","d":"1","n":"6","titel":"Dino\'s Superfeest CD"},{"lied":"Ammenooitnie","performer":"Gebroeders Grimm","sn":"DNCD 3333","d":"1","n":"7","titel":"Dino\'s Superfeest CD"},{"lied":"Het Leven Is Goed In M\'N Brabantse Land","performer":"Oh Sixteen Oh Seven","sn":"DNCD 3333","d":"1","n":"8","titel":"Dino\'s Superfeest CD"}]}';
</script>
<ul id="lijst">
</ul>
<script>
songs = JSON.parse(js);
var lijst = document.getElementById("lijst");
if( songs.Ok )
{
for (var i = 0; i < songs.data.length; i++)
{
song = songs.data[i];
var node = document.createElement("li");
var textnode = document.createTextNode(song.lied + " (" + song.performer + ")");
node.appendChild(textnode);
lijst.appendChild(node);
}
}
</script>
js = '{"Ok":true,"aantalDB":8,"aantalfiles":8,"data":[{"lied":"Hallo Hallo","performer":"Corrie Van Gorp","sn":"DNCD 3333","d":"1","n":"1","titel":"Dino\'s Superfeest CD"},{"lied":"Polonaise Hollandaise (Polon\u00e4se Blankene","performer":"Arie Ribbens","sn":"DNCD 3333","d":"1","n":"2","titel":"Dino\'s Superfeest CD"},{"lied":"\'S Nachts Na Twee\u00ebn","performer":"De Havenzangers","sn":"DNCD 3333","d":"1","n":"3","titel":"Dino\'s Superfeest CD"},{"lied":"Bij Ons Staat Op De Keukendeur","performer":"De Twee Pinten","sn":"DNCD 3333","d":"1","n":"4","titel":"Dino\'s Superfeest CD"},{"lied":"\'K Heb Hele Grote Bloemkoole","performer":"Andr\u00e9 Van Duin","sn":"DNCD 3333","d":"1","n":"5","titel":"Dino\'s Superfeest CD"},{"lied":"Sophia Loren","performer":"Jack De Nijs","sn":"DNCD 3333","d":"1","n":"6","titel":"Dino\'s Superfeest CD"},{"lied":"Ammenooitnie","performer":"Gebroeders Grimm","sn":"DNCD 3333","d":"1","n":"7","titel":"Dino\'s Superfeest CD"},{"lied":"Het Leven Is Goed In M\'N Brabantse Land","performer":"Oh Sixteen Oh Seven","sn":"DNCD 3333","d":"1","n":"8","titel":"Dino\'s Superfeest CD"}]}';
</script>
<ul id="lijst">
</ul>
<script>
songs = JSON.parse(js);
var lijst = document.getElementById("lijst");
if( songs.Ok )
{
for (var i = 0; i < songs.data.length; i++)
{
song = songs.data[i];
var node = document.createElement("li");
var textnode = document.createTextNode(song.lied + " (" + song.performer + ")");
node.appendChild(textnode);
lijst.appendChild(node);
}
}
</script>
Toevoeging op 31/03/2019 14:40:59:
Ik zie op de interwebs dat het Polonäse Blankenese moet zijn. En er ontbreekt een ) achter die titel.
http://www.45cat.com/record/2050735