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

时间:2016-07-22 20:31:47

标签: javascript jquery json

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

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

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

为了使用变量的内容来访问对象的属性,您必须使用所谓的bracket notation

curl -X POST -d '{"id":1, "title":"a title", "body":"a body", "type:1"}' -o log.txt localhost:4000/api/articles

这是因为在Javascript中,对象就像字典一样,它们的属性和方法可以通过键访问,键是属性或方法的名称。

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

ocean_measure.gulf == ocean_measure["gulf"]