Javascript 如何将 json 对象转换为警告框中的字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8895138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:58:34  来源:igfitidea点击:

How to convert a json object to a string in a alert box?

javascriptjqueryjson

提问by marko

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?

我想调试我的响应 (json) 并将其显示为警告框中的字符串。有什么方便的吗?

var myjson = { Name : "Marko" }; 

alert(myjson.toString()); // ? [Object] !!! 

回答by franticfrantic

you can use the following on your success response:

您可以在成功回复中使用以下内容:

alert(JSON.stringify(data));

回答by Armin

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.

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

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

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

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/

但这不是递归的。这是一个工作演示:http: //jsfiddle.net/n695V/

回答by Kristoffer Svanmark

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

您可以使用 JSON.stringify。但是,我不知道它是否适用于所有常见浏览器。

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

警报(JSON.stringify(json-object));

回答by Nicolas Garnil

You can analyze the response text of a request with Firebug or Google Chrome built-in console.

您可以使用 Firebug 或 Google Chrome 内置控制台分析请求的响应文本。

回答by Umesh Patil

You will get the clear answer, if you provide the JSON structure.

如果您提供 JSON 结构,您将得到明确的答案。

For example, if you have below JSON structure.

例如,如果您有以下 JSON 结构。

  var json={ “id”:23,”name”:”marko”};
    for(key in json){
            alert(key+'->'+json[key]);
     }

It's not simple for above single node json. If JSON structure is deeper, you need to iterate till you get value :)

以上单节点json并不简单。如果 JSON 结构更深,则需要迭代直到获得值:)

Or. Rather easiest way is do console.log(json); and see in firebug, you will get what is inside json block.

或者。最简单的方法是执行 console.log(json); 并在萤火虫中看到,您将获得 json 块中的内容。

回答by Eugen Rieck

function JSON2string (jsonobject,prefix) {
  if (!prefix) prefix="";
  if (typeof(jsonobject)=="string") return jsonobject;
  if (typeof(jsonobject)=="number") return jsonobject.toString();
  if (typeof(jsonobject)=="object") {
    var s="Object:\n"
    var newprefix="  "+prefix;
    for (var i in jsonobject) s+=prefix+i+"="+JSON2String(jsonobject[i],newprefix)+"\n";
    return s;
  }
  return "<unhandled>";
}

回答by Shadow

Try this

尝试这个

var myJSONText = JSON.stringify(myObject, replacer);

A JSON stringifier goes in the opposite direction, converting JavaScript data structures into JSON text. JSON does not support cyclic data structures, so be careful to not give cyclical structures to the JSON stringifier. http://www.json.org/js.html

JSON stringifier 则相反,将 JavaScript 数据结构转换为 JSON 文本。JSON 不支持循环数据结构,因此请注意不要为 JSON 字符串化符提供循环结构。http://www.json.org/js.html