如何从javascript变量中检索json数据

时间:2014-03-29 10:40:55

标签: javascript php json

我想了解并使用json。大多数例子都不足以让我停下来,或者进入我想最终做的jQuery或ajax,但是现在,只想了解json的基础知识。这个PHP代码似乎有效,但javascript不是......

<!DOCTYPE html>
<html>
<head>
<title>Connecting MySQL Server</title>
<meta charset="utf-8">
<meta name="author" content="Charlie R Chisholm">
<?php

    $mysqli = new mysqli(dbhost,dbuser,dbpass,dbc);
    if ($mysqli->connect_error) {
        die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
        }

    $query = $mysqli->query("select * from codeTable where tableCode='TABLE' order by Description");
    $json = array();
    if($query->num_rows){
        while($tblArray[] = $query->fetch_object()){
        $json['codeTable'][]=$tblArray;
        }
    }
    mysql_close($mysqli);
    echo json_encode($json);
?>
<script language="JavaScript">
    var tbl = <? echo json_encode($json) ?>;

            // How would I get the Description data in the second row (for example) 
            // the line below is certainly not right                    

    alert(tbl["codeTable"][1].Description); 

</script>

</head>

<body>
</body>
</html> 

我得到了这个结果......

{"codeTable":[[{"codeID":"4","tableCode":"TABLE","Description":"Document Type","Code":"DOCTP","groupCode":"","t1":null,"t2":null,"t3":null,"createDate":"2014-03-28 18:52:06","chaDate":null}],[{"codeID":"4","tableCode":"TABLE","Description":"Document Type","Code":"DOCTP","groupCode":"","t1":null,"t2":null,"t3":null,"createDate":"2014-03-28 18:52:06","chaDate":null},{"codeID":"8","tableCode":"TABLE","Description":"Images","Code":"IMAGE","groupCode":"","t1":"Upload Doc Name","t2":null,"t3":null,"createDate":"2014-03-28 18:57:15","chaDate":null}],[{"codeID":"4","tableCode":"TABLE","Description":"Document Type","Code":"DOCTP","groupCode":"","t1":null,"t2":null,"t3":null,"createDate":"2014-03-28 18:52:06","chaDate":null},{"codeID":"8","tableCode":"TABLE","Description":"Images","Code":"IMAGE","groupCode":"","t1":"Upload Doc Name","t2":null,"t3":null,"createDate":"2014-03-28 18:57:15","chaDate":null},{"codeID":"1","tableCode":"TABLE","Description":"Note Type","Code":"NTYPE","groupCode":"GENRL","t1":"","t2":"","t3":"","createDate":"2014-03-11 18:26:27","chaDate":"0000-00-00 00:00:00"}],[{"codeID":"4","tableCode":"TABLE","Description":"Document Type","Code":"DOCTP","groupCode":"","t1":null,"t2":null,"t3":null,"createDate":"2014-03-28 18:52:06","chaDate":null},{"codeID":"8","tableCode":"TABLE","Description":"Images","Code":"IMAGE","groupCode":"","t1":"Upload Doc Name","t2":null,"t3":null,"createDate":"2014-03-28 18:57:15","chaDate":null},{"codeID":"1","tableCode":"TABLE","Description":"Note Type","Code":"NTYPE","groupCode":"GENRL","t1":"","t2":"","t3":"","createDate":"2014-03-11 18:26:27","chaDate":"0000-00-00 00:00:00"},{"codeID":"5","tableCode":"TABLE","Description":"Projects","Code":"PRJCT","groupCode":"","t1":null,"t2":null,"t3":null,"createDate":"2014-03-28 18:53:14","chaDate":null}]]}

......但是从这里开始,我不知道我去哪里。我尝试用几种不同的符号样式从tbl变量中检索信息,但我似乎得到了#34; undefined&#34;或&#34; [对象]&#34;或&#34; [对象] [对象]&#34;而不是数据。有人可以帮我理解我需要能够从json中检索值的语法吗? (警报框只是一个不知道我在做什么的例子)

2 个答案:

答案 0 :(得分:1)

tbl["codeTable"][1]是一个数组,而不是一个对象。它没有Description属性。

您需要选择阵列中的一个项目。

tbl["codeTable"][1][0]

这是一个具有Description属性的对象。

tbl["codeTable"][1][0].Description


或者,首先不要在其中创建包含一个项目的数组。

while($tblArray[] = $query->fetch_object()){
    $json['codeTable'][]=$tblArray;
}

应该是:

while($myObject = $query->fetch_object()){
    $json['codeTable'][] = $myObject;
}

答案 1 :(得分:-1)

var tbl = <? echo json_encode($json) ?>; 
var content = $.parseJSON(tbl);
$.each(content, function(key, value) {
   alert(key + ": " + value);
});