Google Chrome 将 JSON AJAX 响应显示为树而不是纯文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3888454/
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-09-03 17:35:40  来源:igfitidea点击:

Google Chrome display JSON AJAX response as tree and not as a plain text

ajaxjsongoogle-chromeviewtree

提问by GRboss

I cannot find an answer to this one:

我找不到这个问题的答案:

My AJAX calls return JSON data. In Google Chrome Developer Tools > Resources > XHR when I click on the resource on the left and then on the Content tab I see the JSON string as a string and not as a tree as Firebug and Firebug Lite do.

我的 AJAX 调用返回 JSON 数据。在 Google Chrome 开发人员工具 > 资源 > XHR 中,当我单击左侧的资源,然后单击内容选项卡时,我将 JSON 字符串视为字符串,而不是像 Firebug 和 Firebug Lite 那样的树。

How do I force Chrome to diplay it as a tree. Is there a Content-type that my PHP file must have???

如何强制 Chrome 将其显示为树。是否有我的 PHP 文件必须具有的内容类型???

I would happy to know the answer!

我很高兴知道答案!

Thank you Stefanos

谢谢斯特凡诺斯

回答by Matt McClure

To see a tree view in recent versions of Chrome:

要在最新版本的 Chrome 中查看树视图:

Navigate to Developer Tools > Network > the given response > Preview

导航到开发者工具 > 网络 > 给定的响应 > 预览

回答by Trevor

Google Chrome now supports this (Developer Tools > Network > [XHR item in list] Preview).

Google Chrome 现在支持此 ( Developer Tools > Network > [XHR item in list] Preview)。

In addition, you can use a third party tool to format the json content. Here's one that presents a tree view, and here's another that merely formats the text (and does validation).

此外,您可以使用第三方工具来格式化 json 内容。 这里的一个呈现树状视图,这里的另一种只是格式化文本(和不验证)。

回答by Quentin

The correct content-type for JSON data is application/json. I assume that is what you are missing.

JSON 数据的正确内容类型是application/json. 我认为这就是你所缺少的。

回答by Bingyan Liu

I've found the answer:

我找到了答案:

You MUST encode your json like this: {"c":21001,"m":"p"}but not {c:21001,m:"p"}or {'c':21001,'m':'p'}

你必须像这样编码你的json:{"c":21001,"m":"p"}但不是{c:21001,m:"p"}{'c':21001,'m':'p'}

Thus, the key of a dict must be wrapped in double quotes:", then chrome will preview it as json rather than plain text.

因此,dict 的键必须用双引号括起来:",然后 chrome 会将其预览为 json 而不是纯文本。

回答by Mariana B.

In order for it to display the JSON message properly it has to have the "application/json" mime type and to be correctly structured.

为了正确显示 JSON 消息,它必须具有“application/json”mime 类型并且结构正确。

You can check the structure here

你可以在这里检查结构

回答by JeffBaumgardt

There was an issue with a build of Google Chrome Dev build 24.0.1312.5 that caused the preview panel to no longer display a json object tree but rather flat text. It should be fixed in the next dev

Google Chrome Dev build 24.0.1312.5 版本存在一个问题,导致预览面板不再显示 json 对象树,而是显示纯文本。它应该在下一个开发中修复

See more here: http://code.google.com/p/chromium/issues/detail?id=160733

在此处查看更多信息:http: //code.google.com/p/chromium/issues/detail?id=160733

回答by lambacck

I don't think the Chrome Developer tools pretty print XHR content. See: Viewing HTML response from Ajax call through Chrome Developer tools?

我认为 Chrome 开发人员工具不能很好地打印 XHR 内容。请参阅: 通过 Chrome 开发人员工具查看来自 Ajax 调用的 HTML 响应?

回答by Deborah

I'm not sure if Chrome has added this feature since the last answer, but I was able to view the json response by...

我不确定自上次回答以来 Chrome 是否添加了此功能,但我能够通过以下方式查看 json 响应...

  • opening developer tools in the browser while on the page with the request
  • submitting the request
  • in developer tools - "Console"tab - click on the "Object" tree under the resource to expand
  • 在带有请求的页面上时在浏览器中打开开发人员工具
  • 提交请求
  • 在开发者工具—— “控制台”选项卡——点击资源下的“对象”树展开

...I was able to view the response as a readable hierarchy that showed what to ask for and what is returned. (Neither Network or Resources tab had anything helpful that I was able to find.)

...我能够将响应视为一个可读的层次结构,显示要请求的内容和返回的内容。(网络或资源选项卡都没有我能找到的任何有用的东西。)

Happy requesting!

快乐请求!

回答by Quang Vu

You can use Google Chrome Extension: JSONViewAll formatted json result will be displayed directly on the browser.

您可以使用 Google Chrome 扩展:JSONView所有格式化的 json 结果将直接显示在浏览器上。