我想創建一個嵌套的對象是這樣的:創建一個嵌套的JSON對象動態父母和孩子
[{
"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
},
...........
]
我的代碼:
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));
我越來越喜歡這個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":""}]
在th是結果我得到額外的第二大括號。
問題是什麼?你還不夠清楚 –