Take the 2-minute tour ×

I have a json which is return from the java code. I use toJSON to show the json but it does'nt showing anything and giving an error when i de-bugged it using firebug.Below is the response which i have to show in browser.


below is the function in jsp which i am calling:

        type: 'post',
        success:function(data) {
           var json = $.toJSON(data);
        error:function() {
             alert("request failed");

1st alert shows [object] but 2nd alert is not showing anything.

share|improve this question
could you show the alert(data) output ? –  jbduzan May 14 '12 at 8:09
what kind of ajax call type are you doing? please provide more code –  Fabrizio Calderan May 14 '12 at 8:10
yes sure...it is [object Object] –  Java_NewBie May 14 '12 at 8:11
I have provided the code for ajax as well –  Java_NewBie May 14 '12 at 8:13
@Java_NewBie Please, don't use alert for debugging, use console.log or console.dir with chrome or firebug. –  Yoshi May 14 '12 at 8:21
add comment

closed as not a real question by Quentin, TJHeuvel, Christian, Perception, kapa May 14 '12 at 16:26


1 Answer

up vote 0 down vote accepted

There is no such thing as $.toJSON(), thats why you get an error. Use JSON.stringify() and JSON.parse().

Alternatively, if you set the correct contentType, you should be able to use it without any conversions.

edit: to be correct toJSON() is a plugin. Imo there is no need for this, the standard JSON-handling from the browsers and jQuery is sufficient for that task.

share|improve this answer
code.google.com/p/jquery-json –  Quentin May 14 '12 at 8:28
@Quentin It's a plugin and not part of the standard jQuery. And why use it, when every "normal" browser does this by default. –  Christoph May 14 '12 at 8:36
I'm not saying it should be used, just that the statement There is no such thing as $.toJSON() is incorrect. –  Quentin May 14 '12 at 8:43
JSON.stringify() solves the issue...thanks. –  Java_NewBie May 14 '12 at 8:45
add comment
