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

我试图让用户做出选择,并根据该选择我将深入研究JSON数据并显示所选信息。 最后,我想在Javascript中创建一个下拉选择,然后在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

要使用变量的内容来访问对象的属性,必须使用所谓的括号表示法 :

 ocean_measure.gulf == ocean_measure["gulf"] 

这是因为在Javascript中,对象的行为类似于字典,因为它们的属性和方法可以通过键访问,键是属性或方法的名称。

为了从对象中选择用户作为变量的选择,将变量放在括号中:

 userOcean = "gulf" ocean_measure[userOcean] == ocean_measure["gulf"]