开发者

With JavaScript/jQuery How would I sort/resort this JSON object on the fly to support various displays?

Running over this string.. and Resorting it as needed on the fly

{
"oozie": {
    "admin": {},
    "hosts-list": [
        "1"
    ],
    "hostsinfo": [
        {
            "host-id": "1",
            "details": "Failed ProcessHealthMonitor health check 1 times consecutively",
            "currstatus": "Warning",
            "currstatusclass": "warning"
        },
        {
            "host-id": "2",
            "details": "Failed ProcessHealthMonitor health check 1 times consecutively",
            "currstatus": "Warning",
            "currstatusclass": "warning"
        },
        {
            "host-id": "4",
            "details": "Failed ProcessHealthMonitor health check 1 times consecutively",
            "currstatus": "Warning",
            "currstatusclass": "warning"
        },
        {
            "host-id": "5",
            "details": "Failed ProcessHealthMonitor health check 1 times consecutively",
            "currstatus": "Warning",
            "currstatusclass": "warning"
        }
    ],
    "status": [
        {}
    ]
},
"single-namenode": {
    "admin": {},
    "hosts-list": [
        "1"
    ],
    "hostsinfo": [
        {
            "host-id": "1",
            "details": "Running Service",
            "currstatus": "Running",
            "currstatusclass": "success"
        }
    ],
    "status": [
        {}
    ]
},
"single-database": {
    "admin": {},
    "hosts-list": [
        "1"
    ],
    "hostsinfo": [
        {
            "host-id": "1",
            "details": "Running Service",
            "currstatus": "Running",
            "currstatusclass": "success"
        }
    ],
    "status": [
        {}
    ]
},
"secondarynamenode": {
    "admin": {},
    "hosts-list": [
        "1"
    ],
    "hostsinfo": [
        {
            "host-id": "1",
            "details": "Running Service",
            "currstatus": "Running",
            "currstatusclass": "success"
        }
    ],
    "status": [
        {}
    ]
},
"datanode": {
    "admin": {},
    "hosts-list": [
        "1"
    ],
    "hostsinfo": [
        {
            "host-id": "1",
            "details": "Failed HttpHealthMonitor health check 2 times consecutively",
            "currstatus": "Warning",
            "currstatusclass": "warning"
        }
    ],
    "status": [
        {}
    ]
},
"web": {
    "admin": {},
    "hosts-list": [
        "1"
    ],
    "hostsinfo": [
        {
            "host-id": "1",
            "details": "Setting Master IP",
            "currstatus": "Dead",
            "currstatusclass": "error"
        }
    ],
    "status": [
        {}
    ]
},
"tasktracker": {
    "admin": {},
    "hosts-list": [
        "1"
    ],
    "hostsinfo": [
        {
            "host-id": "1",
            "details": "Running Service",
            "currstatus": "Running",
            "currstatusclass": "success"
        }
    ],
    "status": [
        {}
    ]
},
"jobtracker": {
    "admin": {},
    "hosts-list": [
        "1"
    ],
    "hostsinfo": [
        {
            "host-id": "1",
            "details": "Running Master Service",
            "currstatus": "Running",
            "currstatusclass": "success"
        }
    ],
    "status": [
        {}
    ]
}
}

I have a string similar to that of the above, that changes rather frequently. Using Jquery I want to be able to sort this string on the fly in different mannors. One example Is I want to list all services specific to one host. Or Another is I want to List all services by service type.

Currently this string supports the by service type concept. However I am not really able to get the output to change to my whims so I have to work with what I got. That said whats the best way to sort this string or object I should actually开发者_Python百科 be saying. So that I can show a block that would be all services in one particular host?

I've been trying to do it myself for a couple days and I haven't gotten anywhere I just figure I need a fresh set of eyes on this to give me ideas cause the directions I run seem to road block me..

I have been trying each() in jquery where I nest a few each's down in some trials to do what I want but again I am failing miserably.

EDIT I figured its better to just rebuild the object based on the information I need from the original. I am however getting stuck in gathering that information. Basicly I want the hostsinfo and the main object name of hosts info.

so I would want to return:

oozie = hostid:1, details:.., currstatus:.., currstatusclass:.. oozie = hostid:2, details:.., currstatus:.., currstatusclass:.. oozie = hostid:3, details:.., currstatus:.., currstatusclass:.. (just imagine the above 3 lines in object format)

My Latest Failed attempt. Ive been at this for days and im starting to lose my mind.

$('.refreshAllb').click(function() {
    var outputCon = '';
    $.getJSON('services.json', function(data) {
        $('#master_service_container').empty();
        $.each(data, function(i, object){
            $.each(object, function(property, value){
                if(property == "hostsinfo")
                {
                    $.each(value, function(propertyX, valueX){
                        outputCon += propertyX[valueX] +'<br>';
                    });
                }
            });
        });
        $('#master_service_container').html(outputCon);
    });
});


Looking at your JSON structure, and assuming you have control over it I would change it slightly to make it an array of hosts, with the node name as a property. If you can't change it I would parse it first to make an array that you can work with.

I would then use the LINQ.js jQuery plugin to sort and select as needed.

Check out the linq.js site here:

http://linqjs.codeplex.com/

And the LINQ.js linqpad and API reference here:

http://neue.cc/reference.htm

I'm not quite clear on how you want to sort this JSON. But as an example, lets assume your nodes are in a variable called "nodes" and we want to select the running hosts:

var running = $.Enumerable.From(nodes).Where("$.hostsinfo.currstatus == 'Running'").ToArray()

There are many more query operators that you can use, this is a simple example. Sub queries are also supported.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜