英文:
convert HTML entities in a JSON
问题
<br>
我有一个这种可怕的JSON文件{ "foo": "bar ’ foo ’" }
。
有没有一种方法 - 最好使用jq - 将HTML实体转换成这个示例中的&rsquo;
转换为’
。
我需要以编程方式将它们转换。这个JSON只是一个示例。
谢谢
英文:
<br>
I have a this kind of awful JSON file { "foo": "bar &rsquo; foo &rsquo;" }
.
Is there a way - using preferably jq - to convert the HTML entities then in this example to convert &rsquo;
in ’
.
I need to convert them programmatically. This JSON is only an example.
Thank you
答案1
得分: 1
以下是翻译好的内容:
这是一个易于使用的HTML实体编码器/解码器,称为he
,是用JavaScript编写的。它并非基于jq
,但它应该让您可以相对轻松地完成您尝试的操作。
https://github.com/mathiasbynens/he
安装:
通过npm:
npm install he
通过Bower:
bower install he
通过Component:
component install mathiasbynens/he
在浏览器中:
<script src="he.js"></script>
在Node.js、io.js、Narwhal和RingoJS中:
var he = require('he');
在Rhino中:
load('he.js');
使用像RequireJS这样的AMD加载器:
require(
{
'paths': {
'he': 'path/to/he'
}
},
['he'],
function(he) {
console.log(he);
}
);
作为CLI工具使用:
使用全局标志-g
安装he
npm i -g he
然后,您将能够在命令行中对HTML实体进行编码或解码:
$ he --encode 'föo ♥ bår 𝌆 baz'
> f&#xF6;o &#x2665; b&#xE5;r &#x1D306; baz
$ he --encode --use-named-refs 'föo ♥ bår 𝌆 baz'
> f&ouml;o &hearts; b&aring;r &#x1D306; baz
$ he --decode 'f&ouml;o &hearts; b&aring;r &#x1D306; baz'
> föo ♥ bår 𝌆 baz
编程解码:
he.decode(html, options)
此函数接受一个HTML字符串,并使用HTML规范中第12.2.4.69节中描述的算法解码其中的任何命名和数字字符引用。
he.decode('foo &copy; bar &ne; baz &#x1D306; qux');
// → 'foo © bar ≠ baz 𝌆 qux'
英文:
This is an easy-to-use HTML entity encoder/decoder called he
that was written in JavaScript. It is not jq
based, but it should allow you to do what you are attempting with relative ease.
https://github.com/mathiasbynens/he
INSTALL:
Via npm:
npm install he
Via Bower:
bower install he
Via Component:
component install mathiasbynens/he
In a browser:
<script src="he.js"></script>
In Node.js, io.js, Narwhal, and RingoJS:
var he = require('he');
In Rhino:
load('he.js');
Using an AMD loader like RequireJS:
require(
{
'paths': {
'he': 'path/to/he'
}
},
['he'],
function(he) {
console.log(he);
}
);
Using as a CLI tool:
Install he
using the -g
global flag
npm i -g he
You will be then able to encode or decode HTML entities from the command line:
$ he --encode 'föo ♥ bår 𝌆 baz'
> f&#xF6;o &#x2665; b&#xE5;r &#x1D306; baz
$ he --encode --use-named-refs 'föo ♥ bår 𝌆 baz'
> f&ouml;o &hearts; b&aring;r &#x1D306; baz
$ he --decode 'f&ouml;o &hearts; b&aring;r &#x1D306; baz'
> föo ♥ bår 𝌆 baz
Decoding Programmatically:
he.decode(html, options)
This function takes a string of HTML and decodes any named and numerical character references in it using the algorithm described in section 12.2.4.69 of the HTML spec.
he.decode('foo &copy; bar &ne; baz &#x1D306; qux');
// → 'foo © bar ≠ baz 𝌆 qux'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论