根据用户选择/提示检索JSON数据 [英] retrieve JSON data based on user selection/prompt

查看:54
本文介绍了根据用户选择/提示检索JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to have the user make a choice and based on that choice I will drill into the JSON data and display selected information. Ultimately I would like to create a dropdown selection in html and event listener in Javascript that would then go retrieve.

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数据

above is the Javascript and below is the JSON data

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文件创建数据检索。

I have been unable to find a simple way to take userInput and create a data retrieval with it from JSON file.

推荐答案

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

You got it almost right, but if you want to use a variable while accessing an object you have to do it this way:

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

修复了console.log函数:

Fixed console.log function:

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

这篇关于根据用户选择/提示检索JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆