最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Valid JSON in jsonlint, but JSON.parse() not working - Stack Overflow

programmeradmin5浏览0评论

I have a JSON which is verified in the JSONlint, but I cannot use JSON.parse() as it is not working. What is the problem with the JSON here, if JSON.prase() cannot be used what are my alternatives.

JSON string : "{Products: [{Id: 1,Increment: 5,Max: 1000,Min: 25,allowed: false,Desc: product description,Name:Product Name,Qty: 0}]}"

I have a JSON which is verified in the JSONlint, but I cannot use JSON.parse() as it is not working. What is the problem with the JSON here, if JSON.prase() cannot be used what are my alternatives.

JSON string : "{Products: [{Id: 1,Increment: 5,Max: 1000,Min: 25,allowed: false,Desc: product description,Name:Product Name,Qty: 0}]}"

Share Improve this question asked Feb 13, 2018 at 19:02 Nikhil BharadwajNikhil Bharadwaj 9175 gold badges26 silver badges51 bronze badges 3
  • is there an error message? – epascarello Commented Feb 13, 2018 at 19:03
  • 2 Oh, that is not valid JSON.... none of the keys are quoted. Not sure how that would have passes a JSON linter. – epascarello Commented Feb 13, 2018 at 19:04
  • Yeah, same here, not valid! – Hackerman Commented Feb 13, 2018 at 19:04
Add a ment  | 

3 Answers 3

Reset to default 3

To a JSON be valid, your object keys must be inside double quotes:

{ "validKey": 123 }
  ^        ^
  |        |
  ------------- These double-quotes are required!

JSONLint said that it's alright because you pasted the JSON as you pasted here, wrapped in quotes:

"{Products: [{Id: 1,Increment: 5,Max: 1000,Min: 25,allowed: false,Desc: product description,Name:Product Name,Qty: 0}]}"

And this is a json string with a JSON inside, not a JSON!

If you try to JSONLint without the Quotes you will get this error:

Error: Parse error on line 1:
{   Products: [{        Id: 1
--^
Expecting 'STRING', '}', got 'undefined'

Your strings and keys should be quoted. This is valid JSON that will be correctly parsed by JSON.parse()

{
  "Products": [
    {
      "Id": 1,
      "Increment": 5,
      "Max": 1000,
      "Min": 25,
      "allowed": false,
      "Desc": "product description",
      "Name": "Product Name",
      "Qty": 0
    }
  ]
}

You can read more about the standard here: https://www.json/

This can also happen when you are passing the JSON content inside a "string" variable, you need to using single quotes insted of double quotes of the outside string

var data = "{ "nodes": [{ "id": 1, "text": "DefinitionFinder" },{ "id": 2, "text": "ProjectReference" },{ "id": 3, "text": "ReferenceManager" },{ "id": 4, "text": "ReferenceType" },{ "id": 5, "text": "EventStream" },{ "id": 6, "text": "AutoDisposable" },{ "id": 7, "text": "Handler" }], "links": [{ "from": 1, "to": 3 },{ "from": 2, "to": 4 },{ "from": 3, "to": 1 },{ "from": 3, "to": 2 }] }"

This is the correct way

var data = '{ "nodes": [{ "id": 1, "text": "DefinitionFinder" },{ "id": 2, "text": "ProjectReference" },{ "id": 3, "text": "ReferenceManager" },{ "id": 4, "text": "ReferenceType" },{ "id": 5, "text": "EventStream" },{ "id": 6, "text": "AutoDisposable" },{ "id": 7, "text": "Handler" }], "links": [{ "from": 1, "to": 3 },{ "from": 2, "to": 4 },{ "from": 3, "to": 1 },{ "from": 3, "to": 2 }] }'
发布评论

评论列表(0)

  1. 暂无评论