Reputation: 41
I am trying to create a nested object like this:
[{
"Countrys":{
"CountryCode":"AF",
"CountryName":"AFGHANISTAN",
"CreatedBy":"[email protected]",
"CreatedOn":null,
"ModifiedBy":"Admin",
"ModifiedOn":"/Date(1394094559000)/"
},
"StateCode":"BAM",
"CountryCode":"AF",
"StateName":"BAMIAN",
"CreatedBy":"[email protected]",
"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:
[{"Countrys":{"CountryCode":"AX","CountryName":"ALAND ISLANDS","CreatedBy":"[email protected]","CreatedOn":"","ModifiedBy":"Admin","ModifiedOn":"/Date(1394094559000)/"}},{"StateCode":"NS","CountryCode":"AX","StateName":"NOT SPECIFIED","CreatedBy":"[email protected]","CreatedOn":"/Date(1372617000000)/","ModifiedBy":"","ModifiedOn":""}]
In this result I get extra closing 2nd braces.
Upvotes: 1
Views: 1010
Reputation: 1672
In Javascript, doing:
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;
Upvotes: 1