[英]Error while parsing JSON string using JQuery
我試圖從JSON字符串讀取值並使用JavaScript alert()
語句顯示其中的一些值。 但是我在控制台中遇到異常。
請指導。
控制台例外
SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data
...dc=/\?/;n.parseJSON=function(a){return JSON.parse(a+"")},n.parseXML=function(a){...
at jquery.min.js(line 4, col 5304)
process.js
$(document).ready(function () {
//for handling json data
var json = $("#studentJsonDiv").data("students-json");
console.log(json);
$.each($.parseJSON(json), function (idx, obj) {
alert(obj.name);
});
});
針對home.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="js/process.js"></script>
</head>
<body>
From JQuery (JSON): <div id="studentJsonDiv" data-students-json='${studentsJson}'></div>
</body>
</html>
查看home.jsp的頁面源
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="js/process.js"></script>
</head>
<body>
From JQuery (JSON): <div id="studentJsonDiv" data-students-json='[{"id":1,"name":"Jack"},{"id":2,"name":"Jill"}]'></div>
</body>
</html>
從jQuery 1.6開始, .data()
方法會解析值,因此刪除$.parseJSON()
。 您正在解析導致錯誤的對象而不是字符串。 另外檢查 - 為什么jQuery會自動解析我的data- *屬性?
每次嘗試都將字符串轉換為JavaScript值(這包括布爾值,數字,對象,數組和null)。 如果這樣做不會更改值的表示,則只將值轉換為數字。 例如,“1E02”和“100.000”等同於數字(數值100),但轉換它們會改變它們的表示形式,因此它們將保留為字符串。 字符串值“100”被轉換為數字100。
當data屬性是一個對象(以'{'開頭)或數組(以'['開頭)時,則使用jQuery.parseJSON來解析字符串; 它必須遵循有效的JSON語法,包括引用的屬性名稱。 如果該值不能作為JavaScript值進行解析,則將其保留為字符串。 (摘自https://api.jquery.com/data/ )
$(document).ready(function() { //static message var msg = "Hello World from JQuery!"; $("#mydiv").text(msg); //dynamic message processing for displaying value in div element var students = $("#studentDiv").data("students"); $("#studentDiv").text(students); //for handling json data var json = $("#studentJsonDiv").data("students-json"); // change value here ---------------^------^------ console.log(json); $.each(json, function(idx, obj) { alert(obj.name); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="mydiv"></div> From JQuery: <div id="studentDiv" data-students="[Student{id=1, name=Jack}, Student{id=2, name=Jill}]"></div> From JQuery (JSON): <div id="studentJsonDiv" data-students-json='[{"id":1,"name":"Jack"},{"id":2,"name":"Jill"}]'></div>
您獲得的數據是一個對象數組。 你只需要迭代它,而不必再次解析它。 另外,正確的屬性名稱。
var json = $("#studentJsonDiv").data("students-json");
$.each(json, function (idx, obj) {
alert(obj.name);
});
如果你正在解析
[Student{id=1, name=Jack}, Student{id=2, name=Jill}]
它丟失了:
在學生之后。
您需要在數據中使用students-json
,因為這是您擁有json數據的地方
var json = $("#studentJsonDiv").data("students-json");
$.each($.parseJSON(json), function(idx, obj) {
alert(obj.name);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.