JSON 格式在 JSONLint 上失败
我正在编写一些函数来构造格式正确的 JSON 字符串,然后我可以将其解析为 JSON 对象。
我的对象在 JSONLint 上失败,并出现以下错误:
syntax error, unexpected TINVALID at line 2
[
SERVER: {
ip = 10.10.10.1,
port = 8100
},
TYPES: [
ssh,
shht
]
]
我假设这会给我一个 JavaScript 对象数组。
即使我这样做了(对象而不是数组):
{
SERVER: {
ip = 10.10.10.1,
port = 8100
},
TYPES: [
ssh,
shht
]
}
它不起作用并且我得到相同的错误:
我假设使用该对象,我将能够像这样访问一些数据:
var serverIP = myObject.SERVER.ip;
这当然是我想要的去做。
预先非常感谢,
乔
I'm writing some functions that construct what should be a properly formatted JSON string which I can then parse into a JSON object.
My object is failing on JSONLint with the following error:
syntax error, unexpected TINVALID at line 2
[
SERVER: {
ip = 10.10.10.1,
port = 8100
},
TYPES: [
ssh,
shht
]
]
I assumed that this would give me an array of JavaScript objects.
Even if I did this instead (object instead of array):
{
SERVER: {
ip = 10.10.10.1,
port = 8100
},
TYPES: [
ssh,
shht
]
}
It doesn't work and I get the same error:
I assume that with the object, I would be able to access some data like so:
var serverIP = myObject.SERVER.ip;
That's certainly what I'd like to do.
Many thanks in advance,
Joe
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须在标识符周围使用引号,并且值是字符串:
这是有效的 JSON:
如果您实际上根本没有使用 JSON,而只是尝试创建 Javascript 文字对象,那么您不应该使用 JSONLint 来检查代码。
这是有效的 JavaScript:
You have to use quotation marks around the identifiers, and the values that are strings:
This is valid JSON:
If you are actually not using JSON at all, but just trying to create a Javascript literal object, then you should not use JSONLint to check the code.
This is valid Javascript:
这验证了:
您需要在每个字符串周围使用双引号,并且由于它是一个对象,因此您需要
:
而不是=
This validates:
you need double quotes around every string and since its an object you need
:
instead of=
你将 json 对象与 javascript 数组混合在一起,
这是 json 格式
,这将是一个 JavaScript 数组
操作系统,我认为这就是你正在寻找的
your mixing json object with javascript arrays
this is json format
and this would be a JavaScript array
os I think this is what you are looking for