javascript - Why this JSON object cannot be parsed? - Stack Overflow

In my JavaScript file,The declaring and json parse for the ajax response text is like this:var subcats

In my JavaScript file,

The declaring and json parse for the ajax response text is like this:

var subcats = JSON.parse(this.responseText);

The supposed responseText for the parsing is like this:

{"presubcatId":"1","precatId":"1","presubcatName":"HR Manager"}{"presubcatId":"2","precatId":"1","presubcatName":"Marketing Manager"}

and it gives me this error:

SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 2 column 64 of the JSON data

what's the syntax error? help

In my JavaScript file,

The declaring and json parse for the ajax response text is like this:

var subcats = JSON.parse(this.responseText);

The supposed responseText for the parsing is like this:

{"presubcatId":"1","precatId":"1","presubcatName":"HR Manager"}{"presubcatId":"2","precatId":"1","presubcatName":"Marketing Manager"}

and it gives me this error:

SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 2 column 64 of the JSON data

what's the syntax error? help

Share Improve this question edited Dec 26, 2018 at 12:57 LizardKingLK asked Dec 26, 2018 at 12:53 LizardKingLKLizardKingLK 1314 silver badges9 bronze badges 4
  • 3 Yes, that is not a valid JSON string (or a string at all). You can read the spec here. – pishpish Commented Dec 26, 2018 at 12:56
  • 2 Run it through jsonlint, the error stands out instanly. – Álvaro González Commented Dec 26, 2018 at 12:57
  • 1 I guess it should look like this [{"presubcatId":"1","precatId":"1","presubcatName":"HR Manager"},{"presubcatId":"2","precatId":"1","presubcatName":"Marketing Manager"}] – Rafael Hovsepyan Commented Dec 26, 2018 at 12:57
  • Yes got that. I fetched data by rows which caused Invalid parse I think. Now I tried fetch all method it was okay. – LizardKingLK Commented Dec 26, 2018 at 15:03
Add a ment  | 

6 Answers 6

Reset to default 2

Your JSON have multiple elements and therefore should be wrap in an Array/List like this

[{"presubcatId":"1","precatId":"1","presubcatName":"HR Manager"}{"presubcatId":"2","precatId":"1","presubcatName":"Marketing Manager"}]

Hope it helps

The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string. So you can use it like this if you have a single item:

JSON.parse('{"presubcatId":"1", "precatId":"1", "presubcatName":"HR Manager"}');

but in your case you have multiple items that should be wrapped inside brackets [] and separated by mas or there is SyntaxError:

JSON.parse('[{"presubcatId":"1", "precatId":"1", "presubcatName":"HR Manager" }, { "presubcatId": "2", "precatId": "1", "presubcatName": "Marketing Manager"}]');

var string = '[{"presubcatId":"1", "precatId":"1", "presubcatName":"HR Manager" }, { "presubcatId": "2", "precatId": "1", "presubcatName": "Marketing Manager"}]';

var json = JSON.parse(string);

console.log(json);

Your JSON is invalid. You need change it like this:

[{"presubcatId":"1","precatId":"1","presubcatName":"HR Manager"},{"presubcatId":"2","precatId":"1","presubcatName":"Marketing Manager"}]

your JSON data is invalid so that you are having problem,

var temp=[];
temp=[{"presubcatId":"1","precatId":"1","presubcatName":"HR Manager"},{"presubcatId":"2","precatId":"1","presubcatName":"Marketing Manager"}]

console.log(JSON.stringify(temp))

Your JSON have multiple elements. So it must be treated like Array. See below image.

Below is valid JSON structure.

[{"presubcatId":"1","precatId":"1","presubcatName":"HR Manager"},{"presubcatId":"2","precatId":"1","presubcatName":"Marketing Manager"}]

Your json object is not a valid json. You can check it on this site. It helps me a lot when I need to format or validate a Json object.

https://jsonlint./

Here is your json object formatted and it says what you are missing.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744708354a4589190.html

相关推荐

  • javascript - Why this JSON object cannot be parsed? - Stack Overflow

    In my JavaScript file,The declaring and json parse for the ajax response text is like this:var subcats

    1天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信