我正在尝试创建这个:
var a = {
"requestM" : {
"task" : "list",
"listRequest" : {
"checkedEntryType" : "GLOBAL",
"targetList" : {
"maxResult" : "2",
"status" : "OPEN",
"entryType" : "CALL"
},
"targetList" : {
"maxResult" : "3",
"status" : "CLOSED",
"entryType" : "CALL"
},
"targetList" : {
"maxResult" : "2",
"status" : "OPEN",
"entryType" : "TODO"
},
"targetList" : {
"maxResult" : "2",
"status" : "CLOSED",
"entryType" : "TODO"
}
}
}
}
targetList没有嵌套在数组中,但它重复了。现在我尝试创建这样的消息:
var reqJson = {
"requestM" : {
"task" : "list",
"listRequest" : {
"checkedEntryType" : checkedEntryType
}
}
};
reqJson.requestM.listRequest.targetList={
"maxResult" : 10,
"status" : "OPEN",
"entryType" : "CALL"
};
reqJson.requestM.listRequest.targetList={
"maxResult" : 10,
"status" : "OPEN",
"entryType" : "TODO"
};
然而,第二个targetList将替换第一个。怎么避免这个?谢谢!
答案 0 :(得分:3)
键对于对象必须是唯一的。使其值成为一个数组。从JSON转换为JavaScript对象后,您可以push将数据添加到其中。
"listRequest" : {
"checkedEntryType" : "GLOBAL",
"targetList" : [
{
"maxResult" : "2",
"status" : "OPEN",
"entryType" : "CALL"
},
{
"maxResult" : "3",
"status" : "CLOSED",
"entryType" : "CALL"
}
]
答案 1 :(得分:1)
我认为这不可能像json对象
那样var o =
{
"p1": value1,
"p2": value2
}
p1和p2被认为是对象的属性,因此会覆盖重复的属性。而是尝试使用数组
即
targetList : [ { "maxResult" : "2",
"status" : "OPEN",
"entryType" : "CALL"}, { "maxResult" : "3",
"status" : "CLOSED",
"entryType" : "CALL"}, {..} ]