[英]How can it be I can fetch my title, but not my exercise word from JSON
This is how my JSON looks like 这就是我的JSON的样子
{
"main_object": {
"id": "new",
"getExerciseTitle": "TestToConfirm",
"language": "nl_NL",
"application": "lettergrepen",
"main_object": {
"title": "TestToConfirm",
"language": "nl_NL",
"exercises": [
{
"word": "huisarts",
"syllables": [
"huis",
"arts"
]
}
]
},
"dataType": "json"
}
}
this is how I fetch getExerciseTitle
: 这就是我获取
getExerciseTitle
:
var ID = getUrlParameter("id");
var MEDIAARRAY;
//console.log(ID);
$(document).ready(function () {
$.getJSON('json_files/jsonData_' + ID + '.json', function(json) {
//console.log(json);
var exercisetitle = json.main_object.getExerciseTitle;
$("#getExerciseTitle").html(exercisetitle);
createExercise(json);
});
});
And this is how I am trying to fetch the words
(but this doesn't work, and I am wondering how can this be? 这就是我试图获取
words
(但这不起作用,我想知道这怎么可能?
function createExercise(json) {
var exWord = json.main_object.main_object.exercises.word;
$.each(exWord, function(data, value) {
console.log(exWord);
});
}
My console.log()
doesn't show any error and I have no idea what I am doing wrong since I am using the same method to fetch my exercisetitle
. 我的
console.log()
没有显示任何错误,我也不知道自己在做什么错,因为我使用的是相同的方法来获取我的exercisetitle
。
Because json.main_object.main_object.exercises
is an array , which has no word
property. 因为
json.main_object.main_object.exercises
是一个数组 ,所以没有word
属性。 It has an object as its only entry, and that object has word
, not the array: 它只有一个对象作为其唯一条目,并且该对象具有
word
,而不是数组:
var exWord = json.main_object.main_object.exercises[0].word;
// ------------------------------------------------^^^
or a loop: 或循环:
json.main_object.main_object.exercises.forEach(function(exercise) {
var exWord = exercise.word;
// ...
});
exercises
is an array , not an object. exercises
是一个数组 ,而不是一个对象。 You'll have to access the elements of the array in order to access the object(s) with the word
property. 您必须访问数组的元素才能访问具有
word
属性的对象。
function createExercise(json) {
const exercises = json.main_object.main_object.exercises;
exercises.forEach(function(exercise) {
console.log(exercise.word);
});
}
var json = { "main_object": { "id": "new", "getExerciseTitle": "TestToConfirm", "language": "nl_NL", "application": "lettergrepen", "main_object": { "title": "TestToConfirm", "language": "nl_NL", "exercises": [ { "word": "huisarts", "syllables": [ "huis", "arts" ] } ] }, "dataType": "json" } } $(document).ready(function () { var exercisetitle = json.main_object.getExerciseTitle; $("#getExerciseTitle").html(exercisetitle); var r = json.main_object.main_object.exercises; $.each(r,function(data,value){ console.log(data,value.word) }) });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id='getExerciseTitle'></div>
Because you are trying to access the array element without letting the code know the index of the element. 因为您尝试访问数组元素而不让代码知道元素的索引。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.