使用javascript创建json

时间:2015-08-21 05:00:40

标签: javascript json

我想使用object&创建一个json。 JavaScript的。函数object_merge用于合并对象值。 这是我正在使用的代码。



var dat0 = [{
        "type": "configuration",
        "Process": [{
            "type": "Source",
            "value":  ticket_id
        }],
        "attributes": {

        }
       }];

function object_merge(){
for (var i=1; i<arguments.length; i++)
    for (var a in arguments[i])
        arguments[0][a] = arguments[i][a];
return arguments[0];
 };

var arry = [];
var listobj= [object0,object1,object2,object3,object4,object5,object6,object7,object8,object9];
var object1 = {TicketID: [{value:   ticket_id  }]};
var object2 = {Score:[{value:  ticket_score  }]};
var object3 = {Requestor:[{value:  ticket_requestor_name  }]};
var object4 = {Submitter:[{value:  ticket_submitter_name  }]};
var object5 = {Channel:[{value:  ticket_channel  }]};
var object6 = {Priority:[{value:  ticket_priority  }]};
var object7 = {Status:[{value:  ticket_status  }]};
var object8 = {Subject:[{value:  ticket_subject  }]};
var object9 = {Group:[{value:  ticket_group_name  }]};
var object0 = {TicketType:[{value:  ticket_type  }]};

if ((object1.TicketID[0].value!== (null)||(undefined)))
{
     arry.push(object1);
}
if (object2.Score[0].value!== (null)||(undefined))
{
    arry.push(object2);
}
if (object3.Requestor[0].value!== (null)||(undefined))
{
    arry.push(object3);
}
if (object4.Submitter[0].value!== (null)||(undefined))
{
    arry.push(object4);
}
if (object5.Channel[0].value!== (null)||(undefined))
{
    arry.push(object5);
}
if (object6.Priority[0].value!== (null)||(undefined))
{
    arry.push(object6);
}
if (object7.Status[0].value!== (null)||(undefined))
{
    arry.push(object7);
}
if (object8.Subject[0].value!== (null)||(undefined))
{
    arry.push(object8);
}
if (object9.Group[0].value!== (null)||(undefined))
{
    arry.push(object9);
}
if (object0.TicketType[0].value!== (null)||(undefined))
{
    arry.push(object0);
}
var attr = object_merge(arry);
console.info(JSON.stringify(attr));
dat0[0].attributes = attr;
console.info(JSON.stringify(dat0));
&#13;
&#13;
&#13;

返回类似

的json
[{"type":"configuration","Process":[{"type":"Source","value":902}],"attributes":[{"TicketID":[{"value":902}]},{"Score":[{"value":"unoffered"}]},{"Requestor":[{"value":"raj"}]},{"Submitter":[{"value":"raj"}]},{"Channel":[{"value":"api"}]},{"Status":[{"value":"open"}]},{"Subject":[{"value":"sub"}]},{"Group":[{"value":"Support"}]}]}]

预期结果为

[{"type":"configuration","Process":[{"type":"Source","value":"902"}],"attributes":{"TicketID":[{"value":"902"}],"Score":[{"value":"unoffered"}],"Requestor":[{"value":"raj"}],"Submitter":[{"value":"raj"}],"Channel":[{"value":"api"}],"Status":[{"value":"open"}],"Subject":[{"value":"sub"}],"Group":[{"value":"Support"}]}}]

如何实现它?

2 个答案:

答案 0 :(得分:0)

尝试以下可能对您有帮助的代码。

getView()

答案 1 :(得分:0)

使用此功能代替object_merge

function object_merge(objects) {
    var newObject = {};
    for (var i = 0; i < objects.length; i++)
        for (var key in objects[i])
            newObject[key] = objects[i][key];
    return newObject;
};