gpt4 book ai didi

javascript - 根据用户选择/提示检索 JSON 数据

转载 作者:行者123 更新时间:2023-11-28 05:45:52 26 4
gpt4 key购买 nike

我试图让用户做出选择,并根据该选择深入研究 JSON 数据并显示所选信息。最终,我想在 html 中创建一个下拉选择,并在 Javascript 中创建一个事件监听器,然后进行检索。

var userOcean = prompt("Will you be fishing in the gulf or atlantic ?");

var userFish = prompt("What fish do you want to look up?");

console.log(
"\n\nfish: "+jsonObject.ocean_measure.userOcean.fish.userFish.name+
"\n\nlength: "+jsonObject.ocean_measure.userOcean.fish.userFish.length+
"\n\nclosed: "+jsonObject.ocean_measure.userOcean.fish.userFish.closed+
"\n\nlimit: "+jsonObject.ocean_measure.userOcean.fish.userFish.limit+
"\n\nremarks: "+jsonObject.ocean_measure.userOcean.fish.userFish.remarks
);

上面是 Javascript,下面是 JSON 数据

var jsonObject = {
"ocean_measure" :
{
"gulf":
{
"fish": {
"dolphin": {
"name": "Mahi-mahi",
"length": "none",
"limit": "10 per person or 60 per vessel whichever is less"
},
"blackfin tuna": {
"name": "Blackfin Tuna",
"length": "not regulated",
"limit": "The default bag limit for all unregulated species is two fish or 100 pounds per day, whichever is more"
},
"snook": {
"name": "Snook",
"length": "Not less than 28 inches total length (TL) or more than 33 inches TL",
"closed": "Dec. 1-end of February; May 1-Aug. 31",
"limit": "1 per harvester per day",
"remarks": "Snook permit required for harvest when saltwater license required. State regulations apply in federal waters. Illegal to buy or sell snook. Fish must remain in whole condition until landed ashore (heads, fins, and tails intact). Snatch hooks and spearing prohibited. Harvest prohibited by or with the use of any multiple hook in conjuction with live or dead bait."
}
}
}
,
"atlantic":
{
"fish": {
"dolphin": {
"name": "Mahi-mahi",
"length": "20 inches fork length",
"limit": "10 per person or 60 per vessel whichever is less"
},
"blackfin tuna": {
"name": "Blackfin Tuna",
"length": "not Regulated",
"limit": "The default bag limit for all unregulated species is two fish or 100 pounds per day, whichever is more"
},
"snook": {
"name": "Snook",
"length": "Not less than 28 inches total length (TL) or more than 32 inches TL",
"closed": "Dec. 15 to Jan. 31, June 1 to Aug. 31",
"limit": "1 per harvester per day",
"remarks": "Snook permit required for harvest when saltwater license required. State regulations apply in federal waters. Illegal to buy or sell snook. Fish must remain in whole condition until landed ashore (heads, fins, and tails intact). Snatch hooks and spearing prohibited. Harvest prohibited by or with the use of any multiple hook in conjuction with live or dead bait."
}
}
}
}
}

我一直无法找到一种简单的方法来获取 userInput 并使用它从 JSON 文件创建数据检索。

最佳答案

你说得几乎是对的,但是如果你想在访问对象时使用变量,你必须这样做:

jsonObject.ocean_measure[userOcean].fish[userFish].name

修复console.log功能:

console.log(
"\n\nfish: "+jsonObject.ocean_measure[userOcean].fish[userFish].name+
"\n\nlength: "+jsonObject.ocean_measure[userOcean].fish[userFish].length+
"\n\nclosed: "+jsonObject.ocean_measure[userOcean].fish[userFish].closed+
"\n\nlimit: "+jsonObject.ocean_measure[userOcean].fish[userFish].limit+
"\n\nremarks: "+jsonObject.ocean_measure[userOcean].fish[userFish].remarks
);

此外,JSFiddle

关于javascript - 根据用户选择/提示检索 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38534814/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com