日期:2014-05-16  浏览次数:20436 次

我的json经过在线检验是正确的,为什么javascript读取不到?
我的json经过在线验证是正确的格式。javascript中却不知道如何获取。用alert(data.d)能显示如下数据。我想分别取head里的三个值,Time里的24个值和站名里的24个值,就怎么都获取不到。我用alert(data.head[0].TitleM),就什么都没有显示,我应该如何取这些值呢?
{"head":[{"TitleM":"24小时气温变化图","TitleF":"2014年4月9日8时~2014年4月10日8时","Y":"气温(摄氏度)"}],"Time":[{"Time":"9日8时"},{"Time":"9日9时"},{"Time":"9日10时"},{"Time":"9日11时"},{"Time":"9日12时"},{"Time":"9日13时"},{"Time":"9日14时"},{"Time":"9日15时"},{"Time":"9日16时"},{"Time":"9日17时"},{"Time":"9日18时"},{"Time":"9日19时"},{"Time":"9日20时"},{"Time":"9日21时"},{"Time":"9日22时"},{"Time":"9日23时"},{"Time":"10日0时"},{"Time":"10日1时"},{"Time":"10日2时"},{"Time":"10日3时"},{"Time":"10日4时"},{"Time":"10日5时"},{"Time":"10日6时"},{"Time":"10日7时"}],"content":[{"站名":"2014040909","气温":"18.3"},{"站名":"2014040910","气温":"21"},{"站名":"2014040911","气温":"22.6"},{"站名":"2014040912","气温":"24.5"},{"站名":"2014040913","气温":"26.1"},{"站名":"2014040914","气温":"27.7"},{"站名":"2014040915","气温":"28.5"},{"站名":"2014040916","气温":"28.9"},{"站名":"2014040917","气温":"28"},{"站名":"2014040918","气温":"26.6"},{"站名":"2014040919","气温":"25"},{"站名":"2014040920","气温":"23.7"},{"站名":"2014040921","气温":"22.9"},{"站名":"2014040922","气温":"21.8"},{"站名":"2014040923","气温":"21.1"},{"站名":"2014041000","气温":"20.4"},{"站名":"2014041001","气温":"20.4"},{"站名":"2014041002","气温":"19.5"},{"站名":"2014041003","气温":"19.2"},{"站名":"2014041004","气温":"18.9"},{"站名":"2014041005","气温":"18.3"},{"站名":"2014041006","气温":"18"},{"站名":"2014041007","气温":"18.7"},{"站名":"2014041008","气温":"20.4"}]}
------解决方案--------------------
 需要先将json数据转换成js对象

var a = eval("("+data.d+")");

alert(a.head.TitleM);

------解决方案--------------------
alert(data.d)能显示你那些字符串,说明d属性是string类型的,不是对象

用eval将data.d转为json对象

var d=eval('('+data.d+')')
alert(d.head[0].TitleM)
alert(d.head[0].TitleF)
alert(d.head[0].Y)

------解决方案--------------------

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Untitled Page</title>
</head>
<body>
<script language=JavaScript>
    var data = {"head":[{"TitleM":"24小时气温变化图","TitleF":"2014年4月9日8时~2014年4月10日8时","Y":"气温(摄氏度)"}],"Time":[{"Time":"9日8时"},{"Time":"9日9时"},{"Time":"9日10时"},{"Time":"9日11时"},{"Time":"9日12时"},{"Time":"9日13时"},{"Time":"9日14时"},{"Time":"9日15时"},{"Time":"9日16时"},{"Time":"9日17时"},{"Time":"9日18时"},{"Time":"9日19时"},{"Time":"9日20时"},{"Time":"9日21时"},{"Time":"9日22时"},{"Time":"9日23时"},{"Time":"10日0时"},{"Time":"10日1时"},{"Time":"10日2时"},{"Time":"10日3时"},{"Time":"10日4时"},{"Time":"10日5时"},{"Time":"10日6时"},{"Time":"10日7时"}],"content":[{"站名":"2014040909","气温":"18.3"},{"站名":"2014040910","气温":"21"},{"站名":"2014040911","气温":"22.6"},{"站名":"2014040912","气温":"24.5"},{"站名":"2014040913","气温":"26.1"},{"站名":"2014040914","气温":"27.7"},{"站名":"2014040915","气温":"28.5"},{"站名":"2014040916","气温":"28.9"},{"站名":"2014040917","气温":"28"},{"站名":"2014040918","气温":"26.6"},{"站名":"2014040919","气温":"25"},{"站名":"2014040920","气温":"23.7"},{"站名":"2014040921","气温":"22.9"},{"站名":"2014040922","气温":"21.8"},{"站名":"2014040923","气温":"21.1"},{"站名":"2014041000","气温":"20.4"},{"站名":"2014041001","气温":"20.4"},{"站名":"2014041002","气温":"19.5"},{"站名":"2014041003","气温":"19.2"},{"站名":"2014041004","气温":"18.9"},{"站名":"2014041005","气温":"18.3"},{"站名":"2014041006","气温":"18"},{"站名":"2014041007","气温":"18.7"},{"站名":"2014041008","气温":"20.4"}]};
    alert(data.head[