Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a json object to a string in a alert box? [closed]

I want to debug my response (json) and have it displaying as a string in a alert box. Are there any convenient thing to do?

var myjson = { Name : "Marko" }; 

alert(myjson.toString()); // ? [Object] !!! 
like image 587
marko Avatar asked Jan 17 '12 13:01

marko


People also ask

How do I convert a JSON object to a string?

Use the JavaScript function JSON.stringify() to convert it into a string. const myJSON = JSON.stringify(obj); The result will be a string following the JSON notation.

Does JSON Stringify return a string?

Return Value: It returns a string for a given value. Example: The below is the example of the JSON stringify() Method.

Can JSON be stored as a string?

JSON supports mainly 6 data types:string.

Can I use Backticks in JSON?

Strings use double quotes. No single quotes or backticks in JSON.

When should I use JSON Stringify?

JSON. parse() is used for parsing data that was received as JSON; it deserializes a JSON string into a JavaScript object. JSON. stringify() on the other hand is used to create a JSON string out of an object or array; it serializes a JavaScript object into a JSON string.


3 Answers

you can use the following on your success response:

alert(JSON.stringify(data));
like image 148
franticfrantic Avatar answered Nov 08 '22 00:11

franticfrantic


The most convenient way, would be using the console of your browser.

console.log(json);

In most browsers you get a very clearly view of the json contents.

Alternativly you could make a string with a for-loop:

var output = '';
for (var entry in json) {
  output += 'key: ' + entry + ' | value: ' + json[entry] + '\n';
}
alert(output);

But this is not recursively. Here is a working demonstration: http://jsfiddle.net/n695V/

like image 43
Armin Avatar answered Nov 08 '22 00:11

Armin


You can use JSON.stringify. However, I don't know if it works in all common browsers.

alert(JSON.stringify(json-object));

like image 4
Kristoffer Svanmark Avatar answered Nov 08 '22 00:11

Kristoffer Svanmark