Morgen!
Und zwar habe ich große Probleme, bei der Ausgabe des Inhalts einer JSON-Datei.
Das erste Alert funktioniert, ich kriege "Firstres" ausgegeben.
Das zweite Alert nicht, ich kriege kein "category1" ausgegeben, sondern nur diesen Fehler in der Console:
Uncaught TypeError: Cannot read property '0' of undefined
Mein Script:
Meine JSON-Datei
Und zwar habe ich große Probleme, bei der Ausgabe des Inhalts einer JSON-Datei.
Das erste Alert funktioniert, ich kriege "Firstres" ausgegeben.
Das zweite Alert nicht, ich kriege kein "category1" ausgegeben, sondern nur diesen Fehler in der Console:
Uncaught TypeError: Cannot read property '0' of undefined
Mein Script:
Code:
$(document).ready(function(){
$.getJSON("db_mock.json", function(data){
$.each(data, function(key, value){
alert(value.results[0].cn);
alert(value.category[0].name);
});
});
});
;
Meine JSON-Datei
Code:
[
{"results":[
{"id":"1","cn":"firstres","sub":"first subline","cats":[{"cat":"1"},{"cat":"2"}],"focus":[{"foc":"1"}]},
{"id":"2","cn":"secondres","sub":"second subline","cats":[{"cat":"1"},{"cat":"2"},{"cat":"3"},{"cat":"4"}],"focus":[{"foc":"2"}]},
{"id":"3","cn":"thirdres","sub":"third subline","cats":[{"cat":"4"},{"cat":"5"}],"focus":[{"foc":"2"},{"foc":"3"},{"foc":"4"}]},
{"id":"4","cn":"fourthres","sub":"fourth subline","cats":[{"cat":"3"},{"cat":"4"}],"focus":[{"foc":"3"}]},
{"id":"5","cn":"fifthres","sub":"fifth subline","cats":[{"cat":"1"},{"cat":"3"},{"cat":"4"}]}
]},
{"category":[
{"cat":"1","name":"category1","details":"details about category one","view":"1"},
{"cat":"2","name":"category2","details":"details about category two","view":"2"},
{"cat":"3","name":"category3","details":"details about category three","view":"2"},
{"cat":"4","name":"category4","details":"details about category four","view":"1"},
{"cat":"5","name":"category5","details":"details about category five","view":"3"}
]},
{"views":[
{"view":"1","name":"firstv","details":"this is the first view","location":"top"},
{"view":"2","name":"secondv","details":"this is the second view","location":"left"},
{"view":"3","name":"thirdv","details":"this is the third view","location":"right"}
]},
{"focus":[
{"foc":"1","name":"firstf","details":"this is the first focus","displayed":"FALSE"},
{"foc":"2","name":"secondf","details":"this is the second focus","displayed":"TRUE"},
{"foc":"3","name":"thirdf","details":"this is the third focus","displayed":"TRUE"},
{"foc":"4","name":"fourthf","details":"this is the first focus","displayed":"FALSE"}
]}
]
Zuletzt bearbeitet von einem Moderator: