如何在警报框中将json对象转换为字符串? [英] How to convert a json object to a string in a alert box?

查看:72
本文介绍了如何在警报框中将json对象转换为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想调试我的响应(json),并在警报框中将其显示为字符串.有什么方便的事情吗?

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] !!! 

推荐答案

最方便的方法是使用浏览器的控制台.

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

console.log(json);

在大多数浏览器中,您可以非常清楚地看到json内容.

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

或者,您可以使用for循环创建一个字符串:

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);

但这不是递归的.这是一个有效的演示: http://jsfiddle.net/n695V/

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

这篇关于如何在警报框中将json对象转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆