Click here to Skip to main content
15,894,630 members
Articles / Web Development / ASP.NET

JSON Data Visualizer (for Google, Yahoo!, Bing, and Twitter) using jQuery, JavaScript, ASP.NET MVC 2.0

Rate me:
Please Sign up or sign in to vote.
4.83/5 (14 votes)
29 Jun 2010CPOL6 min read 72.9K   898   46  
This article describes a JSON data visualizer for popular Web Services like Google, Yahoo!, Bing, and Twitter using jQuery, JavaScript, and ASP.NET MVC 2.0.
function BuildTree(obj, parent, levelcount) {
    // Go through all the properties of the passed-in object

    var space = new String("   ");
    var space1 = "";
    var prefix = "";
    var parentprint = "";
    //Setting level counter
    if (parent != undefined) {
        for (var ispace = 0; ispace < levelcount; ispace++) {
            var space1 = space1 + space;
        }

        if (typeof obj == "object") {
            levelcount++;
        }
        else {

            levelcount = 0;
        }
    }
    //start analyzing the object
    for (var i in obj) {
        // if a parent (2nd parameter) was passed in, then use that to 
        // build the message. Message includes i (the object's property name) 
        // then the object's property value on a new line
        var msgi;
        var msgobj;
        if (parent) { msgi = i; msgobj = obj[i]; } else { msgi = msgobj = ""; };
        // If this property (i) is an object, then recursively process the object
        //had to add null test because Google has some null data!
        if ((typeof obj[i] == "object") && (obj[i] != null)) {
            //                  
            if (parent) {
                //

                if ($.isArray(obj)) {
                    folderarray[levelcount] = '[' + i + '].';
                }
                //not Array
                else {
                    folderarray[levelcount] = i +'.';
                }
                //

                BuildTree(obj[i], parent + "." + i, levelcount);

            }
            //not parent  
            else {

                BuildTree(obj[i], i, levelcount);
            }
        }
        // not object
        else {

            parentfolderstring = "";
            for (var ifolder = 2; ifolder < levelcount; ifolder++) 
                         {
                         parentfolderstring += folderarray[ifolder];
                     };
                 //   var stringtoreplace ='.[';
                    var parentfolderstring1 = parentfolderstring.replace(/\.\[/g, "[");
                    //for i in obj continues
                    var parentitem = '<font size="2" color="black"><B>' + space1 + parentfolderstring1;
                   
            
     
            var childitem = msgi + ': </B></font>' + '<font size="2" color="blue">' + msgobj + '</B></font>';
            $('#JsonListdiv').append(parentitem + childitem + '<br/>');
        }
        //end of for i in obj loop
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
Vijay Kumar: Architect, Programmer with expertise and interest in Azure, .net, Silverlight, C#, WCF, MVC, databases and mobile development. Concentrating on Windows Phone 7 and Windows Azure development. Lived in California for many years and done many exciting projects in dotnet and Windows platforms. Moved to Raleigh (RTP), North Carolina recently and available for consulting.  Blog http://Silverazure.blogspot.com.

Comments and Discussions