Javascript 语法错误:JSON.parse:意外字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11759428/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SyntaxError: JSON.parse: unexpected character
提问by Art
I'm having a problem. I have a json code as below. I want to parse them but get an error: SyntaxError: JSON.parse: unexpected character I don't know where is the error. Would anyone help?
我有问题。我有一个 json 代码,如下所示。我想解析它们但得到一个错误: SyntaxError: JSON.parse: 意外字符我不知道错误在哪里。有人会帮忙吗?
My js code:
我的js代码:
function retreive() {
var userInfo = new Array();
userInfo[0] = $("#contactId").val();
userInfo[1] = $("#pw").val();
var cId = $.ajax({
url: "server.php",
type: "POST",
data: {phpData : userInfo},
datatype: "json",
success:function(msg) {
responseJson = JSON.parse(msg.responseText);
var outputHtml = "";
for (var i=0; i<responseJSON.user.mary.length; i++) {
outputHtml += responseJSON.user.mary[i].sender[i].sendDate +
", " + responseJSON.user.mary[i].sender[i].time +
", " + responseJSON.user.mary[i].sender[i].timezone +
", " + responseJSON.user.mary[i].sender[i].message + "<br/>"}
divMessage = document.getElementById("message");
divMessage.innerHTML = outputHtml;
}
});
}
}
my php code:
我的 php 代码:
$data = '{
"user" :
[
{
"mary" :
[
{
"sender1" :
[
{
"sendDate" : "2012-01-13",
"time" : "15:00:21",
"timezone" : "Asia/Hong_Kong",
"message" : "hi"
},
{
"sendDate" : "2012-01-18",
"time" : "16:00:01",
"timezone" : "Asia/Hong_Kong",
"message" : "how are you"
},
{
"sendDate" : "2012-01-21",
"time" : "14:31:42",
"timezone" : "Asia/Hong_Kong",
"message" : "good"
}
],
"sender2" :
[
{
"sendDate" : "2012-01-14",
"time" : "09:01:25",
"timezone" : "Asia/Hong_Kong",
"message" : "good morning"
},
{
"sendDate" : "2012-01-14",
"time" : "09:03:41",
"timezone" : "Asia/Hong_Kong",
"message" : "where are you"
},
{
"sendDate" : "2012-01-14",
"time" : "09:05:42",
"timezone" : "Asia/Hong_Kong",
"message" : "me too"
}
],
}
],
"peter" :
[
{
"sender1" :
[
{
"sendDate" : "2012-01-13",
"time" : "10:44:28",
"timezone" : "Asia/Hong_Kong",
"message" : "hey man"
},
{
"sendDate" : "2012-01-13",
"time" : "10:46:11",
"timezone" : "Asia/Hong_Kong",
"message" : "what are you doing"
},
{
"sendDate" : "2012-01-13",
"time" : "10:48:33",
"timezone" : "Asia/Hong_Kong",
"message" : "nice"
}
],
"sender3" :
[
{
"sendDate" : "2012-01-18",
"time" : "14:23:58",
"timezone" : "Asia/Hong_Kong",
"message" : "Had you send the file to me"
},
{
"sendDate" : "2012-01-18",
"time" : "15:01:39",
"timezone" : "Asia/Hong_Kong",
"message" : "i have not receive yet"
},
{
"sendDate" : "2012-01-19",
"time" : "09:08:32",
"timezone" : "Asia/Hong_Kong",
"message" : "received"
},
],
}
],
}
],
}';
echo $data;
回答by Utkanos
Well, like the error says, your JSON is invalid.
好吧,就像错误所说的那样,您的 JSON 无效。
Constructing JSON manually is a really bad idea. It's far harder to construct, and you're prone to parse errors. Instead, build your data programmatically as an array or object then use json_encode()
.
手动构建 JSON 是一个非常糟糕的主意。构建起来要困难得多,而且您很容易出现解析错误。相反,以编程方式将数据构建为数组或对象,然后使用json_encode()
.