I am creating a graph (GANTT graph) and I need to pass to the plugin a JSON file with all the values to build the graph. The example json for the plugin is this:
source: [{
name: "Example",
desc: "Lorem ipsum dolor sit amet.",
values: [{
to: "/Date(1328832000000)/",
from: "/Date(1333411200000)/",
desc: "Something",
label: "Example Value",
customClass: "ganttRed",
dataObj: foo.bar[i]
}]
},
{
name: "Example1",
desc: "Stackoverflow rulez.",
values: [{
to: "/Date(1328832000000)/",
from: "/Date(1333411200000)/",
desc: "Something else",
label: "Example Value 1",
customClass: "ganttGreen",
dataObj: foo.bar[i]
}]
}]
I added the Example1 element and then tried to validate with JSONLint to be sure I am building the Array in the right way. To my surprise the validator came out saying that the syntax is invalid after the first [{
(obviously I didn't pass source:
to the validator).
The error message I get says:
Error: Parse error on line 1: [{ name: "Example", de --^ Expecting 'STRING', '}', got 'undefined'
What am I doing wrong? Why is the validator raising this error? The Json was written in plain txt format with notepad++ so I cannot think of hidden text in the white spaces
I am creating a graph (GANTT graph) and I need to pass to the plugin a JSON file with all the values to build the graph. The example json for the plugin is this:
source: [{
name: "Example",
desc: "Lorem ipsum dolor sit amet.",
values: [{
to: "/Date(1328832000000)/",
from: "/Date(1333411200000)/",
desc: "Something",
label: "Example Value",
customClass: "ganttRed",
dataObj: foo.bar[i]
}]
},
{
name: "Example1",
desc: "Stackoverflow rulez.",
values: [{
to: "/Date(1328832000000)/",
from: "/Date(1333411200000)/",
desc: "Something else",
label: "Example Value 1",
customClass: "ganttGreen",
dataObj: foo.bar[i]
}]
}]
I added the Example1 element and then tried to validate with JSONLint to be sure I am building the Array in the right way. To my surprise the validator came out saying that the syntax is invalid after the first [{
(obviously I didn't pass source:
to the validator).
The error message I get says:
Error: Parse error on line 1: [{ name: "Example", de --^ Expecting 'STRING', '}', got 'undefined'
What am I doing wrong? Why is the validator raising this error? The Json was written in plain txt format with notepad++ so I cannot think of hidden text in the white spaces
Share Improve this question asked Oct 22, 2018 at 13:17 Lelio FaietaLelio Faieta 6,6969 gold badges48 silver badges84 bronze badges 4- 2 Because that's not valid JSON, it's the JS code for a array of objects. You need to double-quote the property names too to get it to parse properly in JSONLint. – Andy Commented Oct 22, 2018 at 13:20
- you are right! Obvious but didn't notice it. The plugin author was referring to a json and I didn't notice it was not form the beginning – Lelio Faieta Commented Oct 22, 2018 at 13:22
- Note: jsonlint. – Code Spirit Commented Oct 22, 2018 at 13:25
- @CodeSpirit yes, there is where I am validating it – Lelio Faieta Commented Oct 22, 2018 at 13:29
2 Answers
Reset to default 2To pass the data to be a valid JSONLint wrap all the keys in json object as string.
valid json for your input is
"source": [{
"name": "Example",
"desc": "Lorem ipsum dolor sit amet.",
"values": [{
"to": "/Date(1328832000000)/",
"from": "/Date(1333411200000)/",
"desc": "Something",
"label": "Example Value",
"customClass": "ganttRed",
"dataObj": foo.bar[i]
}]
},
{
"name": "Example1",
"desc": "Stackoverflow rulez.",
"values": [{
"to": "/Date(1328832000000)/",
"from": "/Date(1333411200000)/",
"desc": "Something else",
"label": "Example Value 1",
"customClass": "ganttGreen",
"dataObj": foo.bar[i]
}]
}]
wrap you keys in to double quotes. it will work
"source":[
{
"name": "Example",
"desc": "Lorem ipsum dolor sit amet.",
"values": [{
"to": "/Date(1328832000000)/",
"from": "/Date(1333411200000)/",
"desc": "Something",
"label": "Example Value",
"customClass": "ganttRed",
"dataObj": foo.bar[i]
}]
},
{
"name": "Example1",
"desc": "Stackoverflow rulez.",
"values": [{
"to": "/Date(1328832000000)/",
"from": "/Date(1333411200000)/",
"desc": "Something else",
"label": "Example Value 1",
"customClass": "ganttGreen",
"dataObj": foo.bar[i]
}]
}
]
see the below screenshot for valid json
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745288225a4620694.html
评论列表(0条)