如何从jsonString中检索对象?

时间:2014-11-10 13:42:24

标签: javascript jquery json object

Web服务正在返回jsonString。解析后,我必须在表格中绑定它的值。

最简单的方法是找到字符串中的每个对象。 例如:我的字符串以以下格式返回:

{
  "Table1": [
    {
      "ProjectId": "VS200-001---",
      "day1": "---",
      "day2": "---",
      "day3": "---",
      "day4": "---",
      "day5": "---",
      "day6": "---",
      "day7": "---",
      "day8": "---",
      "day9": "---",
      "day10": "---",
      "day11": "---",
      "day12": "---",
      "day13": "4.3",
      "day14": "2",
      "day15": "---",
      "day16": "---",
      "day17": "---",
      "day18": "---",
      "day19": "---",
      "day20": "---",
      "day21": "---",
      "day22": "---",
      "day23": "5",
      "day24": "---",
      "day25": "---",
      "day26": "---",
      "day27": "---",
      "day28": "---",
      "day29": "---",
      "day30": "---"
    },....      
  ]
} 

解析后我得到了对象。

for(var x=0; x< _data.Length;x++)
{
  for(var dys = 0; dys< dates.Length;dys++){
  var val = "day" + (dys+1);
  tbody += _data[0] .val + "</tr>"; 
///Here exception is generating incorrect format
//but if i use here _data[0].day1 it will show the value for day1
//how to convert string to object inorder to retrieve the value

}
}

1 个答案:

答案 0 :(得分:1)

使用存储在变量中的键访问对象时,需要使用数组表示法。改变这个:

_data[0].val

对此:

_data[0][val]

从代码的外观来看,您还需要关闭td