json_encode error “missing ] after element list” with jquery json plugin

February 8th, 2009

If you’re using the excellent jquery json plugin you might run into problems trying to parse json returned from the php function json_encode.

Specifically, when you try to parse the returned json using $.evalJSON you get the javascript error

missing ] after element list

I also had this same problem with the cakephp json component (which I decided to use instead since it would be more portable across php versions than the php5.2+ only json_encode).

The simple fix is this.
Add slashes to your output and surround it with double quotes like this …

$status = json_encode($status);
echo '"'.addslashes($status).'"';

I figured this one out, by examining rails json output that worked just fine with the jquery json plugin.

Entry Filed under: Uncategorized

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Trackback this post  |  Subscribe to the comments via RSS Feed


Calendar

September 2010
S M T W T F S
« Aug    
 1234
567891011
12131415161718
19202122232425
2627282930  

I recommend

Linode VPS's for Rails hosting

Heroku for mindless Rails hosting

Site 5 for shared Rails hosting and all round great service

Most Recent Posts

Categories