1

I keep on getting a JSON parser error for the following data:

[{"data":"Aerospace and Defense"},{"data":"Agriculture"},{"data":"Business Services"},{"data":"Chemicals"},{"data":"Construction"},{"data":"Consumer Goods and Services"},{"data":"Education"},{"data":"Electronics"},{"data":"Energy and Utilities"},{"data":"Environmental Services and Equipment"},{"data":"Financial Services"},{"data":"Food and Beverage"},{"data":"Healthcare Goods and Services"},{"data":"Industrial Goods and Services"},{"data":"Information Technology"},{"data":"Metals and Mining"},{"data":"Security Products and Services"},{"data":"Software"},{"data":"Telecom"},{"data":"Tranportation and Storage"}]

Can anyone please identify what the error is? I was originally using "Aerospace & Defense" and thought that the special character might be the issue. But it doesn't look like it.

I use the ASP.NET MVC JSONResult to pass this back to jQuery. FireFox and IE show the data being created correctly, but the jquery parser throws an error. I have heard that the parser has been modified significantly in the new jQuery.

Any help would be highly appreciated.

asked Feb 27, 2011 at 2:15
3
  • Is the Error Invalid JSON? Or something else? What version of jQuery? Are you calling $.parseJSON? Could you show some code? Commented Feb 27, 2011 at 2:31
  • By "new version" you mean which version exactly? Also, what browser do you use? Commented Feb 27, 2011 at 2:45
  • 3
    Just putting it out there that jQuery's parser doesn't like seeing NaN Commented Nov 28, 2012 at 23:59

7 Answers 7

3

Looks OK to me. See DEMO.

Another demo using this JSON string in a response to an actual jQuery AJAX request in:

I can't really see how the jQuery JSON parser could be significantly modified. This is the actual source of jQuery.parseJSON in jQuery 1.5.1:

parseJSON: function( data ) {
 if ( typeof data !== "string" || !data ) {
 return null;
 }
 // Make sure leading/trailing whitespace is removed (IE can't handle it)
 data = jQuery.trim( data );
 // Make sure the incoming data is actual JSON
 // Logic borrowed from http://json.org/json2.js
 if ( rvalidchars.test(data.replace(rvalidescape, "@")
 .replace(rvalidtokens, "]")
 .replace(rvalidbraces, "")) ) {
 // Try to use the native JSON parser first
 return window.JSON && window.JSON.parse ?
 window.JSON.parse( data ) :
 (new Function("return " + data))();
 } else {
 jQuery.error( "Invalid JSON: " + data );
 }
},

There is only one place that could potentially cause some problems, and that is the JSON regexp:

// JSON RegExp
rvalidchars = /^[\],:{}\s]*$/,
rvalidescape = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,
rvalidtokens = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,
rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g,

but it was last modified in September 2010 by John Resig.

What version are you using, anyway?

answered Feb 27, 2011 at 2:22
Sign up to request clarification or add additional context in comments.

Comments

2

http://www.jsonlint.com/ says it's valid.

Might be a jquery bug?

answered Feb 27, 2011 at 2:18

Comments

1

What version of jQuery are you using? I was having the exact same trouble using the default ASP.NET MVC template in VS 2010 which included a local copy of jQuery 1.5.1.

Even though my returned JSON looked fine and validated I kept getting a 'parserrror' from jQuery $ajax function. Eventually, in desperation, I changed to jQuery 1.6 and it worked fine. So I suspect a bug in 1.5.1.

answered May 12, 2011 at 20:00

1 Comment

I have same issue my jquery was jquery-1.5.2 i replaced it with jquery-1.6.1 and it worked gr8
0

maybe you need single quotes at the beginning and end of json, it should be string since you are parsing it

answered Feb 27, 2011 at 2:23

Comments

0

Thank You for your amazing responses. Here is what I found out after further research. I am using jquery1.5 and turns out there was something very interesting going on. I had a separate post on jsTree because I originally thought it was JSTree that was giving me an error, but turns out this is definitely not jsTree but may be jQuery related.

Due to the nature of the deadline on this project, I haven't been able to research the entire issue, but I have definitely found a workaround. Hope this helps:

jsTree JSON with MVC

answered Feb 27, 2011 at 3:26

Comments

0

Encapsulating the object in squiggly brackets fixed this issue for me. No [] and no "".

answered Aug 14, 2011 at 6:21

Comments

0

One reason I got this error was because I was saving \n (ie "This is my note. \n It tells you something") in my database... I got this from a textarea input. When I tried to send back the json string with that escape character, it threw this error.

answered Mar 21, 2013 at 18:34

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.