jQuery.parseJSON won't work on the following string:
({"stat":"OK","code":400,"data":[{"title":"Development Convention","event_type":false,"dates_and_times":[{"date":"28\/03\/2012","start_time":"10:00 AM","end_time":"10:00 AM"},{"date":"29\/03\/2012","start_time":"10:00 AM","end_time":"10:00 AM"},{"date":"30\/03\/2012","start_time":"12:00 PM","end_time":"12:00 PM"}],"description":"<p>This event will discuss different trends in development.<\/p>\n","featured_image":"<img src=\"http:\/\/mysite\/pesticide-free-organic-meal.jpg\" class=\"attachment-full wp-post-image\" alt=\"bavarian food plates with chicken and wine\" title=\"bavarian food plates with chicken and wine\" \/>","image_gallery":[{"url":"http:\/\/mysite","alt":false}]}]})
Here's where I'm receiving the data:
$.get(
api_url,
{ method: "list_events", venue_id: 73, event_date: 'null' },
function( data ) {
alert(data); // Shows the above string
var response = jQuery.parseJSON( data );
alert(1); // Doesn't appear
alert(response.stat); // Doesn't appear
}
);
Can anyone provide any insight as to why it may not be working? I had read somewhere else on stackoverflow that the backslashes had to be escaped again; replacing all instances of \
with \\
using data = data.replace(/\\/g,"\\\\");
did not resolve the issue.
jQuery.parseJSON won't work on the following string:
({"stat":"OK","code":400,"data":[{"title":"Development Convention","event_type":false,"dates_and_times":[{"date":"28\/03\/2012","start_time":"10:00 AM","end_time":"10:00 AM"},{"date":"29\/03\/2012","start_time":"10:00 AM","end_time":"10:00 AM"},{"date":"30\/03\/2012","start_time":"12:00 PM","end_time":"12:00 PM"}],"description":"<p>This event will discuss different trends in development.<\/p>\n","featured_image":"<img src=\"http:\/\/mysite.\/pesticide-free-organic-meal.jpg\" class=\"attachment-full wp-post-image\" alt=\"bavarian food plates with chicken and wine\" title=\"bavarian food plates with chicken and wine\" \/>","image_gallery":[{"url":"http:\/\/mysite.","alt":false}]}]})
Here's where I'm receiving the data:
$.get(
api_url,
{ method: "list_events", venue_id: 73, event_date: 'null' },
function( data ) {
alert(data); // Shows the above string
var response = jQuery.parseJSON( data );
alert(1); // Doesn't appear
alert(response.stat); // Doesn't appear
}
);
Can anyone provide any insight as to why it may not be working? I had read somewhere else on stackoverflow that the backslashes had to be escaped again; replacing all instances of \
with \\
using data = data.replace(/\\/g,"\\\\");
did not resolve the issue.
- What's worse is that jQuery likes to silently handle errors in the ajax callbacks... If you ran jQuery.parseJSON in the console on that string you would have found 'Unexpected token (' – Matt Esch Commented Apr 8, 2012 at 11:50
-
It is, I changed the service to only do a
json_encode
because I had not been including thecallback
parameter. Working now, thanks. – Matt Commented Apr 8, 2012 at 11:57 - jsonlint. is your friend! – vzwick Commented Apr 8, 2012 at 12:22
3 Answers
Reset to default 5Your string is not valid JSON, the (
and )
are not valid there. It looks like the service is set up to return JSONP.
If you include ?callback=?
in the URL and use $.getJSON
[docs], then jQuery will do all the parsing properly.
Example:
$.getJSON(
api_url + '?callback=?',
{ method: "list_events", venue_id: 73, event_date: 'null' },
function( data ) {
alert(data.stat);
}
);
callback
is the standard parameter name to denote the callback function name, but the service might expect an other one. Refer to its documentation in this case.
Also have a look at $.ajax
[docs] for more information.
The other, but less clean solution would be to trim the start and the end from the parenthesis:
var response = $.parseJSON(data.replace(/^\(|\)$/g, ''));
Of course, if the service is on the same domain and you have control over it, you can simply return JSON.
If you're not sure you can always check your json with http://jsonlint./. In your case, the round brackets () are unnecessary (and incorrect).
this is not a json string ! its an object
a json can start with {
or [
your is an object.