如何在javaScript中访问“值的JSON表示”?
我将json_encode
和echo
结果用于AJAX调用,我得到了PHP手册所指的json对象“json_encode - 返回值的JSON表示”。
这是我控制日志变量
{"RECORD_NUM":1002,"DATE_FIELD":"1747-01-19","DESCRIPTION":"Johann Bode founder of \"Bode's Law \" dies"},
{"RECORD_NUM":1111,"DATE_FIELD":"1770-01-19","DESCRIPTION":"Battle of Golden Hill (Lower Manhattan)"}]
下一步是解析"响应"与JSON.parse
:
returnedArray=JSON.parse(response);
要在JavaScript中访问JSON对象,请使用JSON.parse()解析它,并通过“。”或“[]”访问它。
如果JSON.parse
失败,就像在我的情况下那样,我会问一个问题“你怎么能不能访问返回的值的JSON表示。”?“
这是一个小的html文件,我通过声明一个javaScript变量但没有“值的JSON表示”来测试。
这个文件按预期工作
<!DOCTYPE html><html>
<head>
<title>JSON value</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div>TODO write content</div>
<!--Call a javaScript-->
<script type="text/javascript" src="js/jquery-2.2.0.min.js"></script>
<script>
$("document").ready(function () {
var response = [
{"RECORD_NUM": 967, "DATE_FIELD": "1736-01-19", "DESCRIPTION": "James Watt, inventor of the steam engine, was born in Scotland."},
{"RECORD_NUM": 1002, "DATE_FIELD": "1747-01-19", "DESCRIPTION": "Johann Bode founder of \"Bode's Law \" dies"},
{"RECORD_NUM": 1111, "DATE_FIELD": "1770-01-19", "DESCRIPTION": "Battle of Golden Hill (Lower Manhattan)"}
];
dataCheck = response[0].DATE_FIELD;
console.log("dataCheck = " + dataCheck + "\n");
});
</script>
</body>
</html>
控制台打印:
dataCheck = 1736-01-19
如果我在AJAX成功通话中使用上面的代码,我会收到错误:
success: function (response) {
console.log("Returned data = RESPONSE: \n" + response + "\n\n");
dataCheck = response[0].DATE_FIELD;
console.log("dataCheck = " + dataCheck + "\n");
控制台日志:
queryDatabase02.js:39 Returned data = RESPONSE:
[{"RECORD_NUM":967,"DATE_FIELD":"1736-01-19","DESCRIPTION":"James Watt, inventor of the steam engine, was born in Scotland."},
{"RECORD_NUM":1002,"DATE_FIELD":"1747-01-19","DESCRIPTION":"Johann Bode founder of \"Bode's Law \" dies"},
{"RECORD_NUM":1111,"DATE_FIELD":"1770-01-19","DESCRIPTION":"Battle of Golden Hill (Lower Manhattan)"},
...
}]
所以问题是,&#34;如何在javaScript中访问“值的JSON表示”?&#34; 答案 - &#34;你不能&#34;?
在检查PHP文件中只有一个echo并且还发送头文件后,我发现在echo json_encoded之前删除了头文本 不会导致错误。这是PHP文件的结尾:
// json_encode — Returns the JSON representation of a value
$jsonVal = json_encode($timeRtr);
$fileHandle = fopen('storage\jsonStorage', "a") or die("Unable to open file!");
fwrite($fileHandle, $jsonVal);
fwrite($fileHandle, "\n");
fclose($fileHandle);
//header('Content-Type: application/json');
echo ($jsonVal);
&GT?; 这是javascript文件queryDataBase02.js
$("document").ready(function () {
$("button[name='History']").click(function (e) {
// so the form does not get submitted
e.preventDefault();
var displayStr = "";
var theQuery = {queryString: "SELECT * FROM time_table WHERE" +
" EXTRACT(MONTH FROM DATE_FIELD ) = DATE_FORMAT(NOW()," +
" '%c') AND EXTRACT(DAY FROM DATE_FIELD ) = DATE_FORMAT (NOW (), '%e')" +
" ORDER BY DATE_FIELD]}"};
console.log("\t THEObject = " + theQuery + "\n");
$.ajax({
type: "POST",
// dataType: "json",
url: "queryDatabase02.php",
data: theQuery,
success: function (response) {
console.log("Returned data = RESPONSE: \n" + response + "\n\n");
dataCheck = response[0].DATE_FIELD;
console.log("dataCheck on response " + dataCheck + " \n");
// How big is response
responseCnt = response.length;
console.log("responseCnt = " + responseCnt + "\n");
// Access the data ...
displayArray = JSON.parse(response);
console.log("THIS IS display STRING:\n" + displayArray + "\n");
dataCheck = displayArray[0].DATE_FIELD;
console.log("dataCheck Second time = " + dataCheck + "\n");
console.log("LEAVING .............\n");
}
});
return false;
});
});
这是在PHP文件中有和没有标题的控制台输出。
WITH -- "header('Content-Type: application/json');"
queryDatabase02.js:10 THEObject = [object Object]
queryDatabase02.js:17 Returned data = RESPONSE:
[object Object],[object Object],[object Object],[object Object],
[object Object],[object Object],[object Object],[object Object],
[object Object],[object Object],[object Object],[object Object],
[object Object],[object Object],[object Object],[object Object],
[object Object],[object Object],[object Object],[object Object],
[object Object],[object Object],[object Object],[object Object],
[object Object],[object Object],[object Object],[object Object]
queryDatabase02.js:19 dataCheck on response 1736-01-19
queryDatabase02.js:24 responseCnt = 28
VM211:1 Uncaught SyntaxError: Unexpected token o
$.ajax.success @ queryDatabase02.js:27
i @ jquery-2.2.0.min.js:2
j.fireWith @ jquery-2.2.0.min.js:2
z @ jquery-2.2.0.min.js:4
(anonymous function) @ jquery-2.2.0.min.js:4
==========================================================
没有 - &#34;标题(&#39;内容类型:application / json&#39;);&#34;
THEObject = [object Object]
queryDatabase02.js:17 Returned data = RESPONSE:
[{"RECORD_NUM":967,"DATE_FIELD":"1736-01-19","DESCRIPTION":"James Watt, inventor of the steam engine, was born in Scotland."},
{"RECORD_NUM":1002,"DATE_FIELD":"1747-01-19","DESCRIPTION":"Johann Bode founder of \"Bode's Law \" dies"},
{"RECORD_NUM":1111,"DATE_FIELD":"1770-01-19","DESCRIPTION":"Battle of Golden Hill (Lower Manhattan)"},
....
"}]
queryDatabase02.js:19 dataCheck on response undefined
queryDatabase02.js:24 responseCnt = 4948
queryDatabase02.js:28 THIS IS display STRING:
[object Object],[object Object],[object Object],[object Object],[object Object],
[object Object],[object Object],[object Object],[object Object],[object Object],
[object Object],[object Object],[object Object],[object Object],[object Object],
.... [object Object]
queryDatabase02.js:30 dataCheck Second time = 1736-01-19
queryDatabase02.js:31 LEAVING .............
最后,我注意到有和没有来自PHP文件的头信息的记录计数差异。