I am trying to create a nested object like this:
我试图创建一个这样的嵌套对象:
[{
"Countrys":{
"CountryCode":"AF",
"CountryName":"AFGHANISTAN",
"CreatedBy":"admin@mail.com",
"CreatedOn":null,
"ModifiedBy":"Admin",
"ModifiedOn":"/Date(1394094559000)/"
},
"StateCode":"BAM",
"CountryCode":"AF",
"StateName":"BAMIAN",
"CreatedBy":"admin@mail.com",
"CreatedOn":"/Date(1372617000000)/",
"ModifiedBy":null,
"ModifiedOn":null
},
...........
]
My code :
我的代码:
updateStateList = [];
//state countrys data variable
var stateCountrysCountryCode;
var stateCountrysCountryName;
var stateCountrysCreatedBy;
var stateCountrysCreatedOn;
var stateCountrysModifiedBy;
var stateCountrysModifiedOn;
//state open data variable
var StateCode;
var CountryCode;
var StateName;
var CreatedBy;
var CreatedOn;
var ModifiedBy;
var ModifiedOn;
$(".tableRow").each(function () {
stateCountrysCountryCode = $(this).find("#erSLCountrysCountryCode").val();
stateCountrysCountryName = $(this).find("#erSLCountrysCountryName").val();
stateCountrysCreatedBy = $(this).find("#erSLCountrysCreatedBy").val();
stateCountrysCreatedOn = $(this).find("#erSLCountrysCreatedOn").val();
stateCountrysModifiedBy = $(this).find("#erSLCountrysModifiedBy").val();
stateCountrysModifiedOn = $(this).find("#erSLCountrysModifiedOn").val();
StateCode = $(this).find("#erSLStateCode").val();
CountryCode = $(this).find("#erSLCountryCode").val();
StateName = $(this).find("#erSLStateName").val();
CreatedBy = $(this).find("#erSLCreatedBy").val();
CreatedOn = $(this).find("#erSLCreatedOn").val();
ModifiedBy = $(this).find("#erSLModifiedBy").val();
ModifiedOn = $(this).find("#erSLModifiedOn").val();
CountrysObj = {};
CountrysObj["CountryCode"] = stateCountrysCountryCode;
CountrysObj["CountryName"] = stateCountrysCountryName;
CountrysObj["CreatedBy"] = stateCountrysCreatedBy;
CountrysObj["CreatedOn"] = stateCountrysCreatedOn;
CountrysObj["ModifiedBy"] = stateCountrysModifiedBy;
CountrysObj["ModifiedOn"] = stateCountrysModifiedOn;
//state open data
statesObj = {};
statesObj["StateCode"] = StateCode;
statesObj["CountryCode"] = CountryCode;
statesObj["StateName"] = StateName;
statesObj["CreatedBy"] = CreatedBy;
statesObj["CreatedOn"] = CreatedOn;
statesObj["ModifiedBy"] = ModifiedBy;
statesObj["ModifiedOn"] = ModifiedOn;
//CountrysObj.push(statesObj["StateCode"]);
updateStateList.push({ "Countrys": CountrysObj });
updateStateList.push(statesObj);
});
alert(JSON.stringify(updateStateList));
I am getting like this Json:
我变得像这样Json:
[{"Countrys":{"CountryCode":"AX","CountryName":"ALAND ISLANDS","CreatedBy":"admin@mail.com","CreatedOn":"","ModifiedBy":"Admin","ModifiedOn":"/Date(1394094559000)/"}},{"StateCode":"NS","CountryCode":"AX","StateName":"NOT SPECIFIED","CreatedBy":"admin@mail.com","CreatedOn":"/Date(1372617000000)/","ModifiedBy":"","ModifiedOn":""}]
In this result I get extra closing 2nd braces.
在这个结果中,我得到额外的第二个括号。
1 个解决方案
#1
1
In Javascript, doing:
在Javascript中,做:
x = {}
creates an object. That's why adding (pushing) it to an array creates the result of [{}]
创建一个对象。这就是为什么将它添加(推送)到数组会创建[{}]的结果
What you want is to create an object of all state data, and have countries
as a property of that object.
您想要的是创建所有状态数据的对象,并将国家/地区作为该对象的属性。
something like this:
像这样的东西:
//state open data
statesObj = {};
statesObj["StateCode"] = StateCode;
statesObj["CountryCode"] = CountryCode;
statesObj["StateName"] = StateName;
statesObj["CreatedBy"] = CreatedBy;
statesObj["CreatedOn"] = CreatedOn;
statesObj["ModifiedBy"] = ModifiedBy;
statesObj["ModifiedOn"] = ModifiedOn;
statesObj["Countries"] = CountrysObj;
#1
1
In Javascript, doing:
在Javascript中,做:
x = {}
creates an object. That's why adding (pushing) it to an array creates the result of [{}]
创建一个对象。这就是为什么将它添加(推送)到数组会创建[{}]的结果
What you want is to create an object of all state data, and have countries
as a property of that object.
您想要的是创建所有状态数据的对象,并将国家/地区作为该对象的属性。
something like this:
像这样的东西:
//state open data
statesObj = {};
statesObj["StateCode"] = StateCode;
statesObj["CountryCode"] = CountryCode;
statesObj["StateName"] = StateName;
statesObj["CreatedBy"] = CreatedBy;
statesObj["CreatedOn"] = CreatedOn;
statesObj["ModifiedBy"] = ModifiedBy;
statesObj["ModifiedOn"] = ModifiedOn;
statesObj["Countries"] = CountrysObj;