简体   繁体   English

如何在 VS Code 中验证所选文本为 JSON?

[英]How to validate selected text as JSON in VS Code?

Is there an extension or other solution that allows you to validate selected text as JSON in VS Code?是否有扩展程序或其他解决方案可让您在 VS Code 中将所选文本验证为 JSON?

It's something very simple in concept that I've been doing for years in Notepad++, but surprisingly I still haven't found a way to do this in VS Code.这是我多年来在 Notepad++ 中一直在做的一个非常简单的概念,但令人惊讶的是,我仍然没有找到在 VS Code 中执行此操作的方法。 The three most popular JSON extensions I installed were not able to do this.我安装的三个最流行的 JSON 扩展无法做到这一点。

To validate a JSON text block, I currently have to copy the text, then paste it into something like jsonlint.com to validate.要验证 JSON 文本块,我目前必须复制文本,然后将其粘贴到类似 jsonlint.com 的内容中进行验证。 There's got to be a better way than this in VS Code.在 VS Code 中一定有比这更好的方法。

UPDATE: I'm aware a JSON document is automatically linted in VS Code, but I often work with non-JSON files that have JSON snippets, so it won't work in my situation.更新:我知道 JSON 文档会在 VS Code 中自动进行 linted,但我经常使用具有 JSON 片段的非 JSON 文件,因此它不适用于我的情况。 Here's an example that caused a real issue on production because of the missing comma that wasn't marked with a red squiggly in VS Code:这是一个在生产中导致实际问题的示例,因为在 VS Code 中缺少没有用红色波浪标记的逗号:

[some_template.liquid] [some_template.liquid]

{% schema %}
  {
    ...
    "settings": [
      {
        "type": "header",
        "content": "Content"
      }
      {
        "type": "text",
        "id": "some_id",
        "label": "Some Label"
      }
    ],
    ...
  }
{% endschema %}

Hence, I need a solution that allows me to select a block of text, run a command like "Validate JSON", and have it either auto-correct the selected text, add red squigglies to indicate errors in the JSON, or output a list of errors in the Problems console.因此,我需要一个解决方案,它允许我 select 一个文本块,运行类似“验证 JSON”的命令,并让它自动更正选定的文本,添加红色波浪线以指示 JSON 或 Z713E6221F3983 列表中的错误问题控制台中的错误。

If you create a new file in VScode and select language as "JSON", it should automatically validate JSON text inside.如果您在 VScode 和 select 语言中创建一个新文件作为“JSON”,它应该自动验证 JSON 里面的文本。 You can see problems with the invalid json text in the "Problems" tab in VS code您可以在 VS 代码的“问题”选项卡中看到无效 json 文本的问题

The extension JSON Tools by Erik Lynd allows you to operate on selections. Erik Lynd 的扩展JSON 工具允许您对选择进行操作。

Select a piece of code and call the minify or prettify command (there are keybindings defined), it performs a tryParseJSON call and notifies with an info message. Select 一段代码并调用 minify 或 prettify 命令(定义了键绑定),它执行tryParseJSON调用并通过信息消息通知。 If needed you can add a tryParse only command.如果需要,您可以添加仅tryParse命令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM