Search code examples
jquerygetjson

get data from json file


my code :

  1. file_json.json:
{
    "bio" :
    [
        {
            "id" : "1",
            "name" : "Json"
        },
        {
            "id" : "2",
            "name" : "PHP"
        },
        {
            "id" : "3",
            "name" : "Jquery"
        }
    ]
}

and code html:

<select id="select1">
    <option value="1">1 | Json</option>
    <option value="2">2 | PHP</option>
    <option value="3">3 | Jquery</option>
</select>

<span id="name"></span>

$(document).ready(function(){
    $('select#select1').on('change', function(event){
        $.getJSON('file_json.json', function(data) {
            console.log(data.bio.name);
        });
    });
});

but in console browser undefined, how to fix because i tired on this code ... thanks for any help


Solution

  • bio is an array so you'd need data.bio[0].name