Skip to main content

Iterate through json array using javascript

Javascript Array:

var myArray = {"artists":[{
            "a1":"Adam Sandler",
            "a2":"Adam Lambert",
            "a3":"Avril Levine",
            "a4":"Backstreet Boys",
            "a5":"Blackstreet",
            "a6":"Black Eye Peas",
            "a7":"Cool and the Gang",
            "a8":"Chicago",
            "a9":"Charlie Manson"

        }],
        "songs":[{
            "s1":"Grow Old With You",
            "s2":"Whatdaya Want From Me",
            "s3":"Yea yea",
            "s4":"Quit Playing Games With My Heart",
            "s5":"No Digity",
            "s6":"Meet Me Half way",
            "s7":"Doo wa ditty",
            "s8":"Fight for your honor",
            "s9":"Charlies Song"
        }],
        "genre":[{
            "g1":"Pop",
            "g2":"Pop",
            "g3":"Alternative",
            "g4":"R & B",
            "g5":"R & B",
            "g6":"Hip-Hop",
            "g7":"Funk",
            "g8":"Soft Rock",
            "g9":"Rock"
        }]};


Javascript code to loop only songs:

$.each(myArray.songs, function (i, ob) {
    $.each(ob, function (ind, obj) {
        $('#result').append("key:" + ind + " value:" + obj+"<br/>")
    });
});

Html Div To Paste The output:

<div id="result">
</div>

Output:

key:s1 value:Grow Old With You
key:s2 value:Whatdaya Want From Me
key:s3 value:Yea yea
key:s4 value:Quit Playing Games With My Heart
key:s5 value:No Digity
key:s6 value:Meet Me Half way
key:s7 value:Doo wa ditty
key:s8 value:Fight for your honor
key:s9 value:Charlies Song 

Comments

Popular posts from this blog

Sort Dictionary Based On Value In Asp.Net And C#.Net | Convert Dictionary into KeyValuePair or KeyValuePair into Dictionary.

In this tutorial i am going to explain about how to sort dictionary object based on value in asp.net and C#.Net or convert unsorted dictionary to sorted dictionary object in C#.Net and VB.Net or Convert Dictionary into KeyValuePair or KeyValuePair into Dictionary.

Geckofx Browser in Winform Application

Bored with IE browser in your winform application ? Want to do everything as you doing in your Firefox or Chrome Browser ? Play with automation ? Then here is your choice . Introduction:  GeckoFX is a Windows Forms control written in clean, commented C# that embeds the Mozilla Gecko browser control in any Windows Forms Application. It also contains a simple class model providing access to the HTML and CSS DOM . GeckoFX was originally created by Andrew Young for the fast-growing visual CSS editor, Stylizer . It is now released as open-source under the Mozilla Public License.  You can download it here :   Geckofx 22.0 And the supporting file Xulrunner here :   Xulrunner Files Hope you have downloaded above two files. Here our journey going to start. Create your winform application in visual studio and do the following: right click the toolbox -> Choose items -> Browse the "Geckofx-winforms.dll" and click "yes" for “Load it anyw...

Code to create log files in C#.Net|Asp.Net

Introduction: In my previous article I have explained about how to create, delete and check whether the directory exists using C#.Net . In this article I am going to explain about  How to create log files in C#.Net. Explanation: Log files are useful to track any runtime errors and exceptions in all the applications. Below code will code will get the Message and Pagename as the input and creates the log file in that date. For that first i have imported below two namespaces.