创建多维数组和JSON解析

时间:2015-03-06 08:14:19

标签: javascript json

我在这里有一个小提琴http://jsfiddle.net/prantikv/eqqd6xfm/3/

我有这样的数据

var info={
"company1":[
   {"employee":"*2"},
   {"rooms":"*6"},
   {"vehicals":"3"},
],

"company2":[
    {"employee":"*2"},
    {"rooms":"*6"},
    {"vehicals":"3"},
],
"company3":[
    {"employee":"*2"},
    {"rooms":"*6"},
    {"vehicals":"3"},
]

我从json文件中获取数据。 我想要做的是,我想创建个性化的公司变量,以便我可以在没有其他公司数据的情况下快速加载它们

所以我做的就是这个

var companiesArray=[];
for(company in info){
console.log("company--> "+company);//company1,2,etc 
var detailsArray=[];

for(var i=0;i<info[company].length;i++)   
{
 //loop through the inner array which has the detials
    for(var details in info[company][i]){
     var detailsValue=info[company][i][details];
    detailsArray[details]=detailsValue;
   }
 }

 companiesArray[company]=[company,detailsArray];
}    

的console.log(companiesArray);

因此当我尝试获取数据时,我必须做这样的事情

companiesArray['company1'][1].employee

我想要做的就是这个

companiesArray['company1'].employee

我哪里错了?

4 个答案:

答案 0 :(得分:3)

如果您不想/不能更改JSON,只需更改

即可
detailsArray = []

detailsObject = {}

companiesArray[company]=[company,detailsArray];

companiesArray[company]=detailsObject;

现在你可以使用

companiesArray['company1'].employee

答案 1 :(得分:0)

你必须像这样形成你的json:

var info={
  "company1": {
    "employee":"*2",
    "rooms":"*6",
    "vehicals":"3"
  },    
  "company2": {
    "employee":"*2",
    "rooms":"*6",
    "vehicals":"3"
  },
  "company3": {
    "employee":"*2",
    "rooms":"*6",
    "vehicals":"3"
  }
};

使用花括号({})代替括号([]),以创建可通过点符号访问的对象,如下所示: info.company1.employee

答案 2 :(得分:0)

我同意@Pavel Gatnar和@Guillaume。这是一个非常糟糕的数据结构。如果您只能更改结构,那么您应该使用以下代码。

    var info = {
        "company1": [{
            "employee": "*2"
        }, {
            "rooms": "*6"
        }, {
            "vehicals": "3"
        }],
        "company2": [{
            "employee": "*2"
        }, {
            "rooms": "*6"
        }, {
            "vehicals": "3"
        }],
        "company3": [{
            "employee": "*2"
        }, {
            "rooms": "*6"
        }, {
            "vehicals": "3"
        }]
    };

    var companies = {},
        companyNames = Object.keys(info);

    companyNames.forEach(function (companyName) {
       var companyInfo =  info[companyName],
           company = {};

       companyInfo.forEach(function (properties) {
            var innerPropertyName = Object.keys(properties);

            innerPropertyName.forEach(function (property) {
                company[property] = properties[property];
            });
       });
       companies[companyName] = company;
    });

    console.log(companies);

查看小提琴here

答案 3 :(得分:0)

试试这个:

var info={
    "company1":[
       {"employee":"*2"},
       {"rooms":"*6"},
       {"vehicals":"3"},
    ],

    "company2":[
        {"employee":"*2"},
        {"rooms":"*6"},
        {"vehicals":"3"},
    ],
    "company3":[
        {"employee":"*2"},
        {"rooms":"*6"},
        {"vehicals":"3"},
    ]
};
var companiesArray = {};
for(company in info){
    var detailsArray = {};
    for(var i=0;i<info[company].length;i++) {
        for(var details in info[company][i]){ 
            var detailsValue=info[company][i][details];
            detailsArray[details]=detailsValue;
        }
     }
     companiesArray[company]=detailsArray;
}
console.log(companiesArray['company1'].employee);