js json添加json_在JSON中添加评论的5种方法和工具

news/2024/7/10 2:49:54 标签: python, json, java, vue

js json添加json

In this article, we’ll learn how to use comments in JSON files. We’ll see workarounds and methods used by developers to add single-line and multiple-line comments to their JSON files, the external libraries and packages for stripping comments from your files before feeding them to the regular JSON.parse() method in JavaScript and Node.js and we'll also see simple JavaScript code for removing comments without external libraries. Finally, we'll see the alternative formats to JSON that support comments such as JSON5 and JSONC.

在本文中,我们将学习如何在JSON文件中使用注释。 我们将看到开发人员用于在其JSON文件中添加单行和多行注释的变通办法和方法,以及用于将注释从文件中剥离的外部库和包,然后再将其馈入JavaScript中的常规JSON.parse()方法。和Node.js,我们还将看到简单JavaScript代码,无需外部库即可删除注释。 最后,我们将看到支持注释的JSON替代格式,例如JSON5和JSONC。

JSON不支持注释! (JSON Doesn’t Support Comments!)

As you might be aware of, JSON doesn’t support comments! But as programmers, we are used to add comments so in this article, we’ll see the possible ways that we have to use comments in our JSON files even if they are natively supported by the format.

您可能已经知道,JSON不支持注释! 但是作为程序员,我们习惯于添加注释,因此在本文中,我们将看到在JSON文件中必须使用注释的可能方式,即使该格式本身支持这些注释也是如此。

In fact, comments were not always missing in JSON but were removed later.

实际上,注释并不总是在JSON中丢失,而是在以后被删除。

This is the reason of removing comments from JSON as stated by Douglas Crockford.

这就是Douglas Crockford所说的从JSON删除注释的原因。

I removed comments from JSON because I saw people were using them to hold parsing directives, a practice which would have destroyed interoperability.

我从JSON中删除了注释,因为我看到人们正在使用它们保留解析指令,这种做法会破坏互操作性。

JSON can be mostly needed if you use JSON for your configuration files even if JSON in the first place wasn’t designed for this purpose but for exchanging data but, we see it nowadays used as a configuration format in many apps.

如果您首先将JSON用于配置文件,即使最初不是为了实现此目的而设计JSON也是为了交换数据​​,但如果您将JSON用于配置文件,则最需要使用JSON,但如今,我们将其用作许多应用程序的配置格式。

For example the tsconfig.json file which is the configuration file for TypeScript does allow comments, see microsoft/TypeScript#4987.

例如, tsconfig.json文件(它是TypeScript的配置文件)确实允许注释,请参阅microsoft / TypeScript#4987 。

You also can use comments to comment out values in your data files when testing instead of removing them.

在测试时,您也可以使用注释来注释掉数据文件中的值,而不是删除它们。

As developers we tend always to find a solution for our problems and in case of JSON comments, we also have solutions, let’s explore it.

作为开发人员,我们总是总是为我们的问题找到解决方案,并且在使用JSON注释的情况下,我们也有解决方案,让我们对其进行探讨。

In nutshell, these are the available ways and tools to add comments in JSON:

简而言之,以下是在JSON中添加注释的可用方法和工具:

  • You can add comments as JSON data,

    您可以将注释添加为JSON数据,
  • Google’s GYP supports #-style comments,

    Google的GYP支持#样式的注释,
  • JSON.minify will help you discard C/C++ style comments with JavaScript/Node.js,

    JSON.minify将帮助您使用JavaScript / Node.js放弃C / C ++样式的注释,

  • JSMin is a minification tool for JavaScript that removes comments and unnecessary whitespace from JavaScript files but can be used for JSON files,

    JSMin是JavaScript的缩小工具,可从JavaScript文件中删除注释和不必要的空格,但可用于JSON文件,

  • Hjson: a user interface for JSON that allows you to use comments to document your data inline. You can also use them to comment out values when testing,

    Hjson :JSON的用户界面,允许您使用注释来内联记录数据。 您还可以在测试时使用它们注释掉值,

  • strip-json-comments: It will replace single-line comments // and multi-line comments /**/ with whitespace. This allows JSON error positions to remain as close as possible to the original source. Also available as a Gulp/Grunt/Broccoli plugin,

    strip-json-comments :它将单行注释//和多行注释/**/替换为空格。 这样可以使JSON错误位置尽可能地接近原始源。 也可以作为Gulp / Grunt / Broccoli插件使用 ,

  • comment-json: Parse and stringify JSON with comments in JavaScript/Node.js. It will retain comments even after saved!

    comment-json :使用JavaScript / Node.js中的注释解析和字符串化JSON。 即使保存后,它也将保留评论!

  • nlohmann/json: A JSON parser for modern C++ that provides optional support for ignoring comments on parsing. As stated by the creator of JSON, it’s okay to add comments to your JSON data as long as you use a tool to strip them before parsing the file.

    nlohmann / json :现代C ++的JSON解析器,提供了对忽略解析注释的可选支持。 如JSON的创建者所述,只要在解析文件之前使用工具将其注释掉,就可以在JSON数据中添加注释。

For a detailed explanation, check out how to add comments to JSON.

有关详细说明,请查看如何向JSON添加注释 。

翻译自: https://medium.com/techiediaries-com/5-ways-and-tools-for-adding-comments-in-json-2ea41cbaa628

js json添加json


http://www.niftyadmin.cn/n/1469215.html

相关文章

使用gsap javascript进行动画和React

If there’s one thing I’ve learned from showing my websites to non-technical friends and family it’s this: they‘re never impressed by WebSockets or Ruby Gems or whatever new technology you just learned — they just like the fancy JavaScript animations o…

使用npm安装yarn_如何使用npm yarn node js包json脚本作为构建工具

使用npm安装yarnBundled with the Node.js platform is an under-appreciated feature of the package management tool, npm. The primary purpose for npm is accessing a very large library of packages that run on Node.js. But it includes a feature, the scripts tag,…

开玩笑地嘲笑第三者

React Jest Reacting Testing Library 3rd Party Component MockingReact Jest Reacting测试库第三方组件模拟 I recently ran into an issue where I was trying to test a component that contained a 3rd party component. The child component was more complex than…

当心使用json stringify进行日志记录

Recently I was working on a legacy system built on AWS Lambda and Node.js. It uses the console object along with JSON.stringify() to log messages and data to AWS CloudWatch.最近,我正在开发基于AWS Lambda和Node.js的旧系统。 它使用控制台对象以及JSO…

如何在React中创建地图

Hello readers!各位读者好! Today in this article we will see how to create a map using React, vanilla.js and Leaflet. Before getting into the topic, let’s see about leaflet- it is a powerful tool for building maps. We can create different kinds…

centos6.6 mysql_Centos6.6 下安装mysql

安装mysqlyum -y install mysql-server mysql-devel libcurl-devel net-snmp-devel php php-gd php-xml php-mysql httpdservice mysqld startCentOS 如何修改mysql 用户root的密码第一步:用帐号登录mysql[rootCentOs ~]# mysql -u root或# mysql -uroot -p第二步&…

吊装 分析 软件_我如何理解js中的吊装

吊装 分析 软件Take a look at the code below:看下面的代码: function bigDoll() { function littleDoll() { return i am little } return littleDoll() function littleDoll() { return no I am little! }}bigDoll()What do you think will be the r…

ibatis 获取新增id mysql_ibatis插入数据返回ID的方法

很基础的知识了&#xff0c;贴过来备忘一下 主要就是利用seelctkey来获取这个ID值&#xff0c;但是oracle和mysql的区别还是很大的 oracle的用法 1 <insert id"insertOperation"> 2 <selectKey resultClass"long" keyProperty"Id" >…