Javascript 正在尝试将正确的json数据获取到页面。Js
晚上好。我在一个网站上工作。我正在尝试将下面文件中的json数据发布到页面上。为这首诗创建一个url对我来说很有用。在这一页上我看到了书,章节和诗句。有没有人可以向我解释如何从正确页面上的双韵文中获取文本。如果您只想返回韵文,您需要编写一些内容,以便在数组中找到正确的韵文。所以我会这样做Javascript 正在尝试将正确的json数据获取到页面。Js,javascript,node.js,Javascript,Node.js,晚上好。我在一个网站上工作。我正在尝试将下面文件中的json数据发布到页面上。为这首诗创建一个url对我来说很有用。在这一页上我看到了书,章节和诗句。有没有人可以向我解释如何从正确页面上的双韵文中获取文本。如果您只想返回韵文,您需要编写一些内容,以便在数组中找到正确的韵文。所以我会这样做 app.get('/book/:bibleBook/:bibleChapter/:bibleVerse', (req, res) => { const book = req.params.bibl
app.get('/book/:bibleBook/:bibleChapter/:bibleVerse', (req, res) => {
const book = req.params.bibleBook;
const chapter = req.params.bibleChapter;
const verse = req.params.bibleVerse;
const bibleVerse = [
{
"id": 1001001,
"Book": "Genesis",
"Chapter": 1,
"Verse": 1,
"Text": "In the beginning God created the heaven and the earth."
},
{
"id": 1001002,
"Book": "Genesis",
"Chapter": 1,
"Verse": 2,
"Text": "And the earth was without form, and void; and darkness was upon the face of the deep. And the Spirit of God moved upon the face of the waters."
},
{
"id": 1001003,
"Book": "Genesis",
"Chapter": 1,
"Verse": 3,
"Text": "And God said, Let there be light: and there was light."
},
{
"id": 1001004,
"Book": "Genesis",
"Chapter": 1,
"Verse": 4,
"Text": "And God saw the light, that it was good: and God divided the light from the darkness."
},
{
"id": 1001005,
"Book": "Genesis",
"Chapter": 1,
"Verse": 5,
"Text": "And God called the light Day, and the darkness he called Night. And the evening and the morning were the first day."
}
]
res.send('This is ' + book + ' ' + chapter + ':' + verse);
});
您可能还需要验证从req.params
获得的这些值,以确保您拥有有效的数据
此时,您将拥有匹配的项或foundVerse
的null
对象。因此,您可以编写一些逻辑来发回正确的响应(我假设是文本?)
你能告诉我一个输入和输出的例子吗,我不确定我现在是否理解你了。
let foundVerse = bibleVerse.find(function(verseEl) {
return verseEl.book === book && verseEl.chapter === chapter && verseEl.verse === verse;
});
if (foundVerse) {
return res.send(foundVerse.Text);
}
return res.status(404).send("No verse found.");