Reputation: 2577
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.
Upvotes: 1
Views: 16708
Reputation: 4711
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.
Upvotes: 0
Reputation: 1576
Encapsulating the object in squiggly brackets fixed this issue for me. No [] and no "".
Upvotes: 0
Reputation: 25339
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.
Upvotes: 1
Reputation: 2577
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:
Upvotes: 0
Reputation: 111268
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?
Upvotes: 3
Reputation: 5646
maybe you need single quotes at the beginning and end of json, it should be string since you are parsing it
Upvotes: 0