javascript - What's the difference in using toString() compared to JSON.stringify()? -


in both cases in output content of object:

alert(json.stringify(obj)); 

or

alert(obj.tostring()); 

so... what's difference? advantages or disadvantages of each one?

are there practical examples show difference?

unless have custom object custom .tostring method returning json.stringify of object, there no obj give obj.tostring() == json.stringify(obj).

when obj array [1,2,3] .tostring() gives:

"1,2,3" 

and json.stringify:

"[1,2,3]" 

these close not quite same, json serialized 1 has no ambiguity commas , directly runs javascript or can parsed json.

see:

["1,",2,3].tostring(); //"1,,2,3" ... can't split comma , original array //it in fact impossible restore original array result  json.stringify(["1,",2,3]) //'["1,",2,3]' //original array can restored 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -