我想读取json字符串并输出html页面上的某些字段,并且出现此错误“SyntaxError:JSON.parse:JSON数据第1行第2列的意外字符“ 这是我发送的json。如何正确读取json字符串
{
"Order0" : {
"user" : "Name",
"products" : [ {
"discription" : "freddo",
"productName" : "sketo",
"price" : 22.3,
"id" : 1
}, {
"discription" : "frape",
"productName" : "glyko",
"price" : 22.3,
"id" : 1
}, {
"discription" : "cappouchino",
"productName" : "sketo",
"price" : 22.3,
"id" : 1
} ],
"id" : 0,
"date" : null,
"temp" : "Get"
},
"Order1" : {
"user" : "Name",
"products" : [ {
"discription" : "freddo",
"productName" : "sketo",
"price" : 22.3,
"id" : 1
}, {
"discription" : "frape",
"productName" : "glyko",
"price" : 22.3,
"id" : 1
}, {
"discription" : "cappouchino",
"productName" : "sketo",
"price" : 22.3,
"id" : 1
} ],
"id" : 0,
"date" : null,
"temp" : "Get"
}
}
在此先感谢您。 [编辑]
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
var nIntervId;
function fun() {
nIntervId = setInterval(proxy, 2 * 1000);
}
function proxy() {
$.getJSON('events', function(data) {
var obj = JSON.parse(data);
console.log(obj.toString());
});
}
function stop() {
clearInterval(nIntervId);
}
</script>
</head>
<body onload="fun();">
<div id="data"></div>
</body>
</html>
这是将处理JSON [EDIT2] 与window.alert(JSON.stringify(data));
代码我得到我想要的字符串,但我如何得到某些领域?
有什么不对的JSON作为展示。你确定这是你的浏览器得到的JSON吗?尝试调试您的代码并确保。如果您需要帮助,则需要显示该代码。 –
您分享的代码没有任何问题。在创建它和你试图解析它之间必须要有一些东西。您需要提供[mcve]来演示问题。 – Quentin
你的对象没有任何问题。你在调用'JSON.parse(string)'还是'JSON.stringify(object)'? – vbguyny