为什么这个JSON.parse返回错误:“意外令牌非法”? [英] Why does this JSON.parse return error: "unexpected token illegal"?

查看:141
本文介绍了为什么这个JSON.parse返回错误:“意外令牌非法”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AJAX请求。这是我第一次使用JSON或其中任何一种方法。 ajax实用程序将onreadystatechange回调的参数作为我正在请求的文件的responseText或responseXML返回。使用简单的 info.txt request.responseText 将正常工作,但当我尝试 info时.js JSON.parse 当我多次检查我的语法是否正确时,它返回Unexpected token ILLEGAL。这是JSON:

I'm using an AJAX request. This is the first time I'm using JSON or any of it's methods. The ajax utility returns an argument to the onreadystatechange callback as the responseText or responseXML of the file I'm requesting. Using a simple info.txt and request.responseText will work fine but when I try info.js and JSON.parse it returns "Unexpected token ILLEGAL" when I've checked multiple times that my syntax is correct. Here is the JSON:

JSON:

{
    first: 1,
    second: 2
}


推荐答案

JSON.parse()语法非常严格。键/值对应具有以下形式:

JSON.parse() is very strict in grammar. The key/value pairs should have the form:

string:value

因此first和second应该是JSON对象中的字符串。
将您的JSON更改为以下代码,它应该是正确的

So the "first" and "second" should be string in a JSON object. Change your JSON to following code and it should be right

{
    "first": 1,
    "second": 2
}

这篇关于为什么这个JSON.parse返回错误:“意外令牌非法”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆