2

I have a problem that drives me crazy at the moment. Via JQuery I request a JSON document from the server, which is not accepted (the error is "unexpected character"). However my JSON document seems just fine, in Firebug i.e. the JSON data is correctly detected and parsed.

If I copy the response and try to validate it via jsonlint.com it always fails on the first character ([). If I change some characters and copy stuff around but leaving the result the same it then validates (try for example copying the contents of [] and then erase them and write them again, paste the content again and you have the same document but it now validates). Strange. So I thought it may be related to the encoding, but firefox clearly states that everything is UTF8 encoded.

Now I'm kind of clueless what do to and I hope you can help me in any way.

Here is the JSON reponse of my server:

[
    {
    "id": 67,
    "startdate": "01.01.1970 01:00:00",
    "hometeam": "EHC Muskrats",
    "awayteam": "EHC Vienna Wookies"
} ,         {
    "id": 69,
    "startdate": "01.01.1970 01:00:00",
    "hometeam": "EC Mammuts",
    "awayteam": "EHC Vienna Wookies"
} ,         {
    "id": 70,
    "startdate": "01.01.1970 01:00:00",
    "hometeam": "EHC Vienna Wookies",
    "awayteam": "EC Mammuts"
} ,         {
    "id": 86,
    "startdate": "01.01.1970 01:00:00",
    "hometeam": "EHC Vienna Wookies",
    "awayteam": "EC Mammuts"
} ,         {
    "id": 4,
    "startdate": "08.10.2012 21:00:00",
    "hometeam": "Wiener Teifl",
    "awayteam": "EHC Vienna Wookies"
} ,         {
    "id": 18,
    "startdate": "28.10.2012 21:00:00",
    "hometeam": "EC Heizbären",
    "awayteam": "EHC Vienna Wookies"
} ,         {
    "id": 19,
    "startdate": "04.11.2012 18:00:00",
    "hometeam": "EHC Vienna Wookies",
    "awayteam": "Innviertel Penguins"
} ,         {
    "id": 6,
    "startdate": "15.11.2012 21:00:00",
    "hometeam": "EHC Vienna Wookies",
    "awayteam": "EC Heizbären"
} ,         {
    "id": 62,
    "startdate": "17.11.2012 19:30:00",
    "hometeam": "EC V.U. Totonka",
    "awayteam": "EHC Vienna Wookies"
} ,         {
    "id": 12,
    "startdate": "22.11.2012 20:30:00",
    "hometeam": "EHC Vienna Wookies",
    "awayteam": "EHC Muskrats"
} ,         {
    "id": 13,
    "startdate": "27.11.2012 19:30:00",
    "hometeam": "TÜV Cannibals",
    "awayteam": "EHC Vienna Wookies"
} ,         {
    "id": 7,
    "startdate": "02.12.2012 17:00:00",
    "hometeam": "Fire Fighters jun.",
    "awayteam": "EHC Vienna Wookies"
} ,         {
    "id": 63,
    "startdate": "03.12.2012 19:00:00",
    "hometeam": "",
    "awayteam": "EHC Vienna Wookies"
} ,         {
    "id": 8,
    "startdate": "09.12.2012 17:00:00",
    "hometeam": "EHC Vienna Flames",
    "awayteam": "EHC Vienna Wookies"
} ,         {
    "id": 14,
    "startdate": "17.12.2012 20:15:00",
    "hometeam": "EC Mammuts",
    "awayteam": "EHC Vienna Wookies"
} ,         {
    "id": 15,
    "startdate": "07.01.2013 20:15:00",
    "hometeam": "EHC Vienna Wookies",
    "awayteam": "EC Mammuts"
} ,         {
    "id": 9,
    "startdate": "10.01.2013 20:00:00",
    "hometeam": "EHC Vienna Wookies",
    "awayteam": "EHC Chiefs"
} ,         {
    "id": 11,
    "startdate": "19.01.2013 21:00:00",
    "hometeam": "EC Attacki",
    "awayteam": "EHC Vienna Wookies"
} ,         {
    "id": 16,
    "startdate": "24.01.2013 20:30:00",
    "hometeam": "EHC Vienna Wookies",
    "awayteam": "TÜV Cannibals"
} ,         {
    "id": 10,
    "startdate": "28.01.2013 19:30:00",
    "hometeam": "EHC Vienna Wookies",
    "awayteam": "EC V.U. Totonka"
} ,         {
    "id": 17,
    "startdate": "30.01.2013 20:30:00",
    "hometeam": "EHC Muskrats",
    "awayteam": "EHC Vienna Wookies"
} ,         {
    "id": 65,
    "startdate": "24.02.2013 21:00:00",
    "hometeam": "EC Heizbären",
    "awayteam": "EHC Vienna Wookies"
} ,         {
    "id": 66,
    "startdate": "28.02.2013 20:40:00",
    "hometeam": "EHC Vienna Wookies",
    "awayteam": "EHC Chiefs"
} ,         {
    "id": 64,
    "startdate": "08.03.2013 21:00:00",
    "hometeam": "EHC Vienna Flames",
    "awayteam": "EHC Vienna Wookies"
}     ]
4

3 回答 3

3

在 ID 67 之前,你有.JSON。如果你删除它。它会有效。虽然这里没有出现。但是如果你在这里尝试,你会. (dot)在 id 67 之前 看到一个额外的内容。检查 JSON。. (dot)ID 67 前删除

  1. 在使用 Json 之前,请在服务器端正确解析它。
于 2013-03-11T11:31:36.313 回答
3

JSON 第一行的换行符有问题。如果您无法在服务器上修复它,请尝试在解析 javascript 中的 JSON 响应之前替换这些字符。

于 2013-03-11T11:30:32.180 回答
0

感谢 Ravi 为我指明了正确的方向。JSON 中有一个不可见的字符破坏了 JQuery JSON 解析器。

服务器端的问题是我的所有文件中的 1(!) 编码为 UTF8 + BOM :-< 可能早就想到了。更改编码解决了它。

于 2013-03-11T15:08:17.810 回答