在javascript中动态解析json对象

时间:2022-08-26 11:28:16

I have a JSON object similar to this

我有一个类似于此的JSON对象

{
  emp_name:'',
  day1:'',
  day2:'',
  day3:'',
  .....
  dayn:''
}

I want to get this value dynamically using javascript and display it in a table. Below is what I am trying to do.

我想使用javascript动态获取此值并将其显示在表中。以下是我想要做的事情。

for(var i = 0; i < 2; i++)
{
  var row1  = body.insertRow(i);
  var name  = resultset[i].emp_name;
  var cell1 = row1.insertCell(0);

  cell1.innerHTML=name;
  cell1.setAttribute('id', 'emp_name');

  for(var j = 1; j < 32; j++)
  {
    var cell2=row1.insertCell(j);
    cell2.innerHTML = resultset[i].day + j;
  }
}

But the second cell value is not getting populated. I am getting NaN. The problem is because of day + j. Can someone say how to approach this?

但第二个单元格值没有填充。我正在拿NaN。问题是因为day + j。有人可以说如何处理这个问题吗?

4 个解决方案

#1


As I understood, you want to access the property, which is day{n}, right?

据我所知,您想要访问该属性,即{n}天,对吗?

If so, you have to change

如果是这样,你必须改变

resultset[i].day+j;

To

resultset[i]['day'+j];

#2


The code you have is identical to (resultset[i].day) + j - that means take the value of (nonexistant) day property from resultset[i], and add j to id. That's why you get a NaN.

你拥有的代码与(resultset [i] .day)+ j相同 - 这意味着从resultset [i]获取(nonexistant)day属性的值,并将j添加到id。这就是你得到NaN的原因。

Whould you should be doing is:

你应该做的是:

 for(var j=1;j<32;j++){
      var cell2=row1.insertCell(j);
        cell2.innerHTML=resultset[i]['day'+j];
 }

#3


If one day, the number of properties or items change, you can loop in your dynamic object without changing your code :

如果有一天,属性或项目的数量发生变化,您可以在不更改代码的情况下循环动态对象:

for (i = 0, l=resultSet.length; i < l; i++) {
    var row1 = body.insertRow(i);
    var result = resultSet[i];
    var name = result.emp_name;

    var keys = Object.keys(result);
    for (var j = 1, m=keys.length; j < m; j++) {
        var cell2 = row1.insertCell(j);
        cell2.innerHTML = result[keys[j]];
    }
}

#4


Since you have tagged jquery too, I have given the answer using jquery. You can accomplish it using $.each function of jquery.

既然你也标记了jquery,我已经使用jquery给出了答案。你可以使用jquery的$ .each函数来完成它。

$.each(jsonString,function(key,value){
//You can perform your operations here
console.log(key+'=>'+value); 

});

Visit here for more info jQuery.each

访问此处获取更多信息jQuery.each

#1


As I understood, you want to access the property, which is day{n}, right?

据我所知,您想要访问该属性,即{n}天,对吗?

If so, you have to change

如果是这样,你必须改变

resultset[i].day+j;

To

resultset[i]['day'+j];

#2


The code you have is identical to (resultset[i].day) + j - that means take the value of (nonexistant) day property from resultset[i], and add j to id. That's why you get a NaN.

你拥有的代码与(resultset [i] .day)+ j相同 - 这意味着从resultset [i]获取(nonexistant)day属性的值,并将j添加到id。这就是你得到NaN的原因。

Whould you should be doing is:

你应该做的是:

 for(var j=1;j<32;j++){
      var cell2=row1.insertCell(j);
        cell2.innerHTML=resultset[i]['day'+j];
 }

#3


If one day, the number of properties or items change, you can loop in your dynamic object without changing your code :

如果有一天,属性或项目的数量发生变化,您可以在不更改代码的情况下循环动态对象:

for (i = 0, l=resultSet.length; i < l; i++) {
    var row1 = body.insertRow(i);
    var result = resultSet[i];
    var name = result.emp_name;

    var keys = Object.keys(result);
    for (var j = 1, m=keys.length; j < m; j++) {
        var cell2 = row1.insertCell(j);
        cell2.innerHTML = result[keys[j]];
    }
}

#4


Since you have tagged jquery too, I have given the answer using jquery. You can accomplish it using $.each function of jquery.

既然你也标记了jquery,我已经使用jquery给出了答案。你可以使用jquery的$ .each函数来完成它。

$.each(jsonString,function(key,value){
//You can perform your operations here
console.log(key+'=>'+value); 

});

Visit here for more info jQuery.each

访问此处获取更多信息jQuery.each