2016-07-22 73 views
1

我想让用户做出选择,并根据该选择我将钻入JSON数据并显示选定的信息。最终,我想创建一个下拉选择的HTML和JavaScript中的事件监听器,然后将检索。上述根据用户选择/提示检索JSON数据

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文件,它的数据检索。

回答

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

+0

如果你想在这里工作的话,就是这个问题的链接。 http://stackoverflow.com/questions/38545522/html-select-value-passed-into-javascript-var-then-used-to-fetch-json – Carl

0

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

ocean_measure.gulf == ocean_measure["gulf"] 

这源于一个事实,即在Javascript中,对象像在自己的属性和方法可以用按键来访问字典,关键是属性或方法的名称。

为了让用户的选择,作为一个变量,从对象选择,放在括号中的变量:我有一个“第二部分:”如果你将这个小应用程序我

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