What I think about better approach is:
{
"author": "No Name",
"title": "Wonderful Journey",
"publishedDate": "17-03-2019",
"country": "German",
"city": "Berlin",
"publisher": "Publisher Name",
"chapters": [
{
"id": 1,
"title": "Journey Begin",
"pages": [
{
"id": 1,
"picture": "City_Building.png",
"content": "Something else...",
"footnote": "Something important..."
},
{
"id": 2,
"picture": "City_Building.png",
"content": "Something else...",
"footnote": "Something important..."
}
]
}
]
}
You can save it as JSON file like book.json and call JSON file from Javascript like this:
var book = undefined,
getBook = new XMLHttpRequest();
getBook.onreadystatechange(function() {
if(this.readyState == 4 && this.status == 200) {
book = JSON.parse(this.responseText);
// get author of book
console.log(book.author);
// get content of chapter 1 page 1
console.log(book.chapters[0].pages[0].content);
// get footnote of chapter 1 page 2
console.log(book.chapters[0].pages[1].footnote);
};
getBook.open('GET', 'book.json');
getBook.send();
For information about ajax you can follow this link.
Answer from A Naive Dreamer on Stack OverflowWhat I think about better approach is:
{
"author": "No Name",
"title": "Wonderful Journey",
"publishedDate": "17-03-2019",
"country": "German",
"city": "Berlin",
"publisher": "Publisher Name",
"chapters": [
{
"id": 1,
"title": "Journey Begin",
"pages": [
{
"id": 1,
"picture": "City_Building.png",
"content": "Something else...",
"footnote": "Something important..."
},
{
"id": 2,
"picture": "City_Building.png",
"content": "Something else...",
"footnote": "Something important..."
}
]
}
]
}
You can save it as JSON file like book.json and call JSON file from Javascript like this:
var book = undefined,
getBook = new XMLHttpRequest();
getBook.onreadystatechange(function() {
if(this.readyState == 4 && this.status == 200) {
book = JSON.parse(this.responseText);
// get author of book
console.log(book.author);
// get content of chapter 1 page 1
console.log(book.chapters[0].pages[0].content);
// get footnote of chapter 1 page 2
console.log(book.chapters[0].pages[1].footnote);
};
getBook.open('GET', 'book.json');
getBook.send();
For information about ajax you can follow this link.
You can't do
var lastSentence = chapter.page.sentence.text
because sentence in your structure is an array, so when you want to access text property, you need to specify the index: chapter.page.sentence[index].text
If you want to get text from the latest sentence, you can use .length property of array.
var lastSentence = chapter.page.sentence[chapter.page.sentence.length - 1].text
Note, that I used length - 1 because array indexes start from 0, e.g. Array [1, 2, 3] has 3 elements with indexes 0, 1, 2