我如何用JavaScript或jQuery重建这个JSON对象
How would I rebuild this JSON Object with JavaScript or jQuery
{
"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": [
{}
]
}
}
我花了好几天想弄明白这个问题,但就是搞不懂。我无法控制实际的JSON对象及其输出。然而,我对它有一个完全不同的需求,在它目前的结构中,除了给我带来问题之外什么也没有。
我想用jQuery做的最好是
获取Main对象的名称Oozie、Single-namenode、single-database、secondarynamenode等…并按照更实用的格式重新构建存储对象。
最后我想看到的是一个看起来与
相似的新对象。{
"myservices":{[
{"name":"oozie", "host-id": "1", "details":"failed process health monitor....", "currstatus":"Warning", "currstatusclass":"warning"},
{"name":"oozie", "host-id": "2", "details":"failed process health monitor....", "currstatus":"Warning", "currstatusclass":"warning"},
{"name":"oozie", "host-id": "3", "details":"failed process health monitor....", "currstatus":"Warning", "currstatusclass":"warning"},
{"name":"oozie", "host-id": "4", "details":"failed process health monitor....", "currstatus":"Warning", "currstatusclass":"warning"},
{"name":"oozie", "host-id": "5", "details":"failed process health monitor....", "currstatus":"Warning", "currstatusclass":"warning"},
{"name":"single-namenode", "host-id": "2", "details":"failed process health monitor....", "currstatus":"Warning", "currstatusclass":"warning"}
]}
我最近一次失败的尝试是:
$('.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);
});
});
正如我所说的,我已经做了好几天了,我已经无计可施了,任何帮助都是非常感谢的。
下面是一个例子。注意,所需的输出是无效的JSON。在您的示例中,myservices
属性不能是对象和数组。它应该是一个对象(关联数组)或一个标准javascript数组:
var input = ... the original object
var output = { myservices: [] };
for (var key in input) {
if (input.hasOwnProperty(key)) {
for (var i = 0, hostsinfo = input[key].hostsinfo; i < hostsinfo.length; i++) {
output.myservices.push({
'name': key,
'host-id': hostsinfo[i]['host-id'],
'details': hostsinfo[i]['details'],
'currstatus': hostsinfo[i]['currstatus'],
'currstatusclass': hostsinfo[i]['currstatusclass']
});
}
}
}
// TODO: use the output
现场演示。
尝试:
function restructure(data) {
var result = {myservices: []}, i, name;
for (name in data) {
if (data.hasOwnProperty(name) && data[name].hostsinfo !== undefined) {
for (i = 0; i < data[name].hostsinfo.length; i += 1) {
result.myservices.push($.extend({name: name}, data[name].hostsinfo[i]));
}
}
}
return result;
}
// with data being your input data
console.log(data, restructure(data));
var result = {};
var myservices = [];
$.each(data.oozie.hostsinfo,function(key,value){
var temp = {}
temp.name = "oozie";
temp.host-id = value.host-id;
temp.details = value.details;
temp.currstatus = value.currstatus;
temp.currstatusclass = value.currstatusclass;
myservices.push(temp);
})
$.each(data.single-namenode.hostsinfo,function(key,value){
var temp = {}
temp.name = "single-namenode";
temp.host-id = value.host-id;
temp.details = value.details;
temp.currstatus = value.currstatus;
temp.currstatusclass = value.currstatusclass;
myservices.push(temp);
})
result.myservices = myservices;
相关文章:
- jQuery匹配JSON对象的部分文本
- 如何在Javascript中将JSon对象转换为数组
- 我可以在json对象中添加一个函数吗
- 使用JS将数组转换为json对象
- 如何为json对象中的段发送array[]
- 将JSON对象传递给angular指令
- 更改JSON对象的结构
- 访问JSON对象内部的数组元素
- 在ejs-partial中对JSON对象进行迭代
- 遍历 JSON 对象并检查 URL 是否以某个值结尾
- 访问嵌套JSON对象的键,其中键是动态的
- json对象中缺少对象循环
- 发送json对象或使用express路由呈现视图
- 在play2框架中向json对象添加下拉列表项
- 元素名称上带有短划线 (-) 字符的 Json 对象
- autocomplete不接受源的json对象
- 如何在javascript中创建动态json对象
- 在使用客户端脚本时拾取JSON对象
- 如何通过json对象数组为嵌套对象赋值
- 构造JSON对象