How to read from json file

in the next Example there are two file. when you run the json.html you need to open the debugger in chrome and look in the consol tab to see the result.

json.html


<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>

    $.ajax({
        url: 'myFirstJson.json',
        dataType: 'json',
        type: 'get',
        cache: false,
        success: function(data)
        {
            $(data.articles).each(function(index, value)
                                    {
                                        console.log(value.id);
                                        console.log(value.name);
                                        console.log(value);
                                    }
                                 )
        }
    })
</script>

</body>
</html>

myFirstJson.json


{
    "articles":[
        {
            "id": 1,
            "name": "how to build a website"
        },
        {
            "id": 2,
            "name": "asaf"
        },
        {
            "id": 3,
            "name": "shay"
        }
    ]
}
Advertisements
This entry was posted in כללי and tagged , , . Bookmark the permalink.

One Response to How to read from json file

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s