使用JQuery解析JSON字符串时出错
Error while parsing JSON string using JQuery
我试图从JSON字符串读取值,并使用JavaScript alert()
语句显示它的一些值。但是我在控制台得到以下异常。
请指导。<<p> 控制台异常/strong>
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"answers"100.000"作为数字是等价的(数值100),但是转换它们会改变它们的表示,因此它们将作为字符串保留。字符串值"100"被转换成数字100。
当数据属性是一个对象(以'{'开头)或数组(以'['开头),则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);
});
您需要在数据中使用students-json
,因为这是您拥有json数据的地方
var json = $("#studentJsonDiv").data("students-json");
$.each($.parseJSON(json), function(idx, obj) {
alert(obj.name);
});
如果你在解析
[Student{id=1, name=Jack}, Student{id=2, name=Jill}]
Student后面缺少:
相关文章:
- 比较Javascript中的两个字符串时出错
- 尝试组合字符串时出错
- 解析从js发送到Java的json字符串时出错
- 在javascript中将字符串转换为int时出错
- 根据64字符串绘制画布图片时出错
- angularjs[ng:areq]参数'fn'不是函数,在控制台中获取字符串时出错
- WebSQL:处理SQL时出错:数量为''语句字符串中的s与参数计数不匹配
- 在 AngularJS 中将字符串转换为对象时出错
- NaN Javascript 和 Jquery 尝试连接数字和字符串时出错
- 将对象转换为字符串 Javascript 时出错
- 在 ES6 中扩展字符串时出错
- 将字符串中的方括号文字与JS正则表达式匹配时出错
- 将字符串变量传递给方法时出错
- NodeJS Bug?执行在要返回的字符串之前带有换行符的返回语句时出错
- 在javascript中连接utf字符串时出错
- 转义字符串时出错
- 敲除js时出错字符串不是函数
- JQuery比较字符串时出错
- 使用JQuery解析JSON字符串时出错
- 将字符串转换为json对象时出错