0

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.com\/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.com","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.

3
  • 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 (' 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 the callback parameter. Working now, thanks. Commented Apr 8, 2012 at 11:57
  • jsonlint.com is your friend! Commented Apr 8, 2012 at 12:22

3 Answers 3

5

Your 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.

Sign up to request clarification or add additional context in comments.

2 Comments

Is there any reason why I should use JSONP instead of JSON? I wrote the web services and have been able to execute only json_encode instead; no ( ) or callback parameter needed.
I should have guessed that you are accessing a service on the same domain, otherwise it would not have worked at all. No, of course if the service is on the same domain, simply use JSON. You can still use $.getJSON though to let jQuery parse the response automatically.
1

If you're not sure you can always check your json with http://jsonlint.com/. In your case, the round brackets () are unnecessary (and incorrect).

Comments

0

this is not a json string ! its an object

a json can start with { or [

your is an object.

1 Comment

I assume this is the response the server sends, not actually inside the code. So it's just some text...

Your Answer

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.