2016-07-22 8 views
1

ユーザーに選択させようとしています。その選択に基づいて、JSONデータをドリルダウンして選択した情報を表示します。最終的には、JavascriptのHTMLおよびイベントリスナーでドロップダウン選択を作成し、それを取得したいと考えています。上記ユーザーの選択/プロンプトに基づいて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." 
       } 
      } 
     } 
    } 
} 

である私は、ユーザ入力を取り、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"] 
関連する問題