动态创建嵌套的JSON对象父级和子级

时间:2016-05-02 06:55:44

标签: javascript json object

我正在尝试创建一个这样的嵌套对象:

[{
   "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
    },
    ...........
]

我的代码:

    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":"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":""}]

在这个结果中,我获得了额外的第二个大括号。

1 个答案:

答案 0 :(得分:1)

在Javascript中,执行:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8"> 
  </head>
  <body>
    <button type="button">1</button>
    <button type="button">2</button>
    <button type="button">3</button>
    <button type="button">4</button>
    <button type="button">5</button>
    <button type="button">6</button>
    <br/>
    <p>Last 3 buttons clicked: none</p>
  </body>
</html>

创建一个对象。这就是为什么将它添加(推送)到数组会产生结果的原因     [{}]

您想要的是创建所有状态数据的对象,并将x = {} 作为该对象的属性。

类似的东西:

countries