未捕获的syntaxerror意外令牌U JSON

uncaught syntaxerror unexpected token U JSON

当我在chrome中运行页面时,我得到了这个错误"uncaught syntaxerror unexpected token u"。在Firefox中,我得到了"json.parse:unexpected character"。我从一个PHP文件返回JSON数据,返回的JSON字符串是有效的。我在http://jsonlint.com/上查过。如有任何帮助,我们将不胜感激…谢谢。

这是返回的json字符串

1
2
3
4
5
6
7
8
9
10
[
    ["1","Pan Africa Market",""1521 1st Ave, Seattle, WA"","47.608941","-122.340145","restaurant"],
    ["2","The Melting Pot","14 Mercer St, Seattle, WA","47.624562","-122.356442","restaurant"],
    ["3","Ipanema Grill","1225 1st Ave, Seattle, WA","47.606366","-122.337656","restaurant"],
    ["4","Sake House","230 1st Ave, Seattle, WA","47.612825","-122.34567","bar"],
    ["5","Crab Pot","1301 Alaskan Way, Seattle, WA","47.605961","-122.34036","restaurant"],
    ["6","Mexican Kitchen","2234 2nd Ave, Seattle,WA","47.613975","-122.345467","bar"],
    ["7","Wingdome","1416 E Olive Way, Seattle, WA","47.617215","-122.326584","bar"],
    ["8","Piroshky Piroshky","1908 Pike pl, Seattle, WA","47.610127","-122.342838","restaurant"]
]


当给予JSON.parse的值实际上是undefined时,通常会出现这种错误。所以,我将检查试图解析这个的代码——很可能您没有解析这里显示的实际字符串。


我在验证(在MVC项目中)时收到此消息。对于我来说,添加validationmessagefor元素修复了该问题。

准确地说,jquery.validate.unobtrusive.js中的第43行导致了以下问题:

1
  replace = $.parseJSON(container.attr("data-valmsg-replace")) !== false;


发生此错误的最常见情况是使用生成控件的模板,然后更改"重写"默认模板生成id和/或name的方式,例如

1
@Html.TextBoxFor(m => m, new {Name = ViewData["Name"], id = ViewData["UniqueId"]} )

然后忘了把ValidationMessageFor改成

1
@Html.ValidationMessageFor(m => m, null, new { data_valmsg_for = ViewData["Name"] })

希望这能节省你一些时间。


json.parse的参数可能没有返回任何内容(即,为json.parse提供的值是undefined

在我分析xyz.sol文件中编译的solidity代码时发生了这种情况。

1
2
3
4
5
6
7
8
9
import web3 from './web3';
import xyz from './build/xyz.json';

const i = new web3.eth.Contract(
  JSON.parse(xyz.interface),
  '0x99Fd6eFd4257645a34093E657f69150FEFf7CdF5'
);

export default i;

拼写错误为

1
JSON.parse(xyz.intereface)

什么也没还!


当我在for循环中的jsonarray上运行find条件时,遇到了这个错误。我面临的问题是for循环中某个值返回空值的结果。因此,当我试图访问某个属性时,它失败了。

因此,如果您在JSONARRAYS中做任何不确定数据源及其完整性的事情,我认为在这种情况下处理空的和未定义的异常是一个好习惯。

我通过检查JSONarray上find的返回值是否为空并适当地处理异常来修复它。

我想这可能有帮助。


在我的例子中,它试图在XhrResponse返回之前对Ajax变量调用json.parse()。如:

1
2
var response = $.get(URL that returns a valid JSON string);
var data = JSON.parse(response.responseText);

我用jquery站点中的一个例子替换了它,得到了$.get:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<script type="text/javascript">
    var jqxhr = $.get("https://jira.atlassian.com/rest/api/2/project", function() {
          alert("success" );
        })
          .done(function() {
//insert code to assign the projects from Jira to a div.
                jqxhr = jqxhr.responseJSON;
                console.log(jqxhr);
                var div = document.getElementById("products");
                for (i = 0; i < jqxhr.length; i++) {
                    console.log(jqxhr[i].name);
                    div.innerHTML +="Product:" + jqxhr[i].name +"<BR/>Key:" + jqxhr[i].key +"<BR/>";
                }
                console.log(div);
            alert("second success" );
          })
          .fail(function() {
            alert("error" );
          })
          .always(function() {
            alert("finished" );
          });

        // Perform other work here ...

        // Set another completion function for the request above
        jqxhr.always(function() {
          alert("second finished" );
        });


Well, to all who actually can't find the error anywhere in your code, neither"undefined as it says in local storage nor null"..simply just comment out your code and write another one that actually removes the item in local storage ..after that ,u can comment or delet the current code and reset again the previous one by simply uncommenting it out (if u dint delet t ...if u did u can write it again:))

1
2
3
4
5
LocalStorage.setItem('Previous' ,"removeprevious");  


LocalStorage.removeItem('Previous');  
 Console.log(LocalStorage.getItem('Previous'));

控制台将显示空值,如果不工作,请重新设置代码,伙计!你有错误。

对不起我的英语!


这不是一项艰巨的任务。这个问题也发生在我的网站上,你应该把你的JS文件按顺序移动。因为在使用JSON解析的地方,这次不会加载JS文件。例α

1
2
3
4
<script type="text/javaScript">
...........SOME CODE.............

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript">

改为

1
2
3
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript">
<script type="text/javaScript">
...........SOME CODE.............

如果你不明白

例如,假设我有一个JSON字符串,而不是JSON对象或数组。

所以如果在javascript中,将字符串解析为

1
2
3
4
5
6
7
8
9
10
11
12
var body={
 "id": 1,
 "deleted_at": null,
 "open_order": {
   "id": 16,
   "status":"open"}

var jsonBody = JSON.parse(body.open_order); //HERE THE ERROR NOW APPEARS BECAUSE THE STRING IS NOT A JSON OBJECT YET!!!!
//TODO SO
var jsonBody=JSON.parse(body)//PASS THE BODY FIRST THEN LATER USE THE jsonBody to get the open_order

var OpenOrder=jsonBody.open_order;

上面的答案很好


当我对json字符串使用相同的变量并解析json时,我得到了这个错误:

[cc]var json = '{"1":{"url":"somesite1","poster":"1.png","title":"site title