2016-05-02 83 views
1

我想創建一個嵌套的對象是這樣的:創建一個嵌套的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是結果我得到額外的第二大括號。

+0

問題是什麼?你還不夠清楚 –

回答

0

在Javascript中,這樣做的:

x = {} 

創建一個對象。這就是爲什麼添加(推)到一個數組創建的 結果[{}]

你想要的是創建所有狀態數據的對象,並有countries爲對象的屬性。

是這樣的:

//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; 
+0

謝謝#Itai Bar-Haim。 – SoloThink

+0

很高興能幫到@SoloThink。在StackOverflow中,通常通過upvoting來標記有用的帖子和/或將有用的答案標記爲已接受的答案。 –