将HTML实体转换为JSON中的内容。

huangapple go评论62阅读模式
英文:

convert HTML entities in a JSON

问题

<br>
我有一个这种可怕的JSON文件{ "foo": "bar &rsquo; foo &rsquo;" }

有没有一种方法 - 最好使用jq - 将HTML实体转换成这个示例中的&amp;rsquo;转换为

我需要以编程方式将它们转换。这个JSON只是一个示例。

谢谢

英文:

<br>
I have a this kind of awful JSON file { &quot;foo&quot;: &quot;bar &amp;rsquo; foo &amp;rsquo;&quot; }.

Is there a way - using preferably jq - to convert the HTML entities then in this example to convert &amp;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

在浏览器中:

&lt;script src=&quot;he.js&quot;&gt;&lt;/script&gt;

在Node.js、io.js、Narwhal和RingoJS中:

var he = require(&#39;he&#39;);

在Rhino中:

load(&#39;he.js&#39;);

使用像RequireJS这样的AMD加载器:

require(
  {
    &#39;paths&#39;: {
      &#39;he&#39;: &#39;path/to/he&#39;
    }
  },
  [&#39;he&#39;],
  function(he) {
    console.log(he);
  }
);

作为CLI工具使用:

使用全局标志-g安装he

npm i -g he

然后,您将能够在命令行中对HTML实体进行编码或解码:

$ he --encode &#39;f&#246;o ♥ b&#229;r &#119558; baz&#39;
&gt; f&amp;#xF6;o &amp;#x2665; b&amp;#xE5;r &amp;#x1D306; baz

$ he --encode --use-named-refs &#39;f&#246;o ♥ b&#229;r &#119558; baz&#39;
&gt; f&amp;ouml;o &amp;hearts; b&amp;aring;r &amp;#x1D306; baz

$ he --decode &#39;f&amp;ouml;o &amp;hearts; b&amp;aring;r &amp;#x1D306; baz&#39;
&gt; f&#246;o ♥ b&#229;r &#119558; baz

编程解码:

he.decode(html, options)

此函数接受一个HTML字符串,并使用HTML规范中第12.2.4.69节中描述的算法解码其中的任何命名和数字字符引用。

he.decode(&#39;foo &amp;copy; bar &amp;ne; baz &amp;#x1D306; qux&#39;);
// → &#39;foo &#169; bar ≠ baz &#119558; qux&#39;
英文:

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:

&lt;script src=&quot;he.js&quot;&gt;&lt;/script&gt;

In Node.js, io.js, Narwhal, and RingoJS:

var he = require(&#39;he&#39;);

In Rhino:

load(&#39;he.js&#39;);

Using an AMD loader like RequireJS:

require(
  {
    &#39;paths&#39;: {
      &#39;he&#39;: &#39;path/to/he&#39;
    }
  },
  [&#39;he&#39;],
  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 &#39;f&#246;o ♥ b&#229;r &#119558; baz&#39;
&gt; f&amp;#xF6;o &amp;#x2665; b&amp;#xE5;r &amp;#x1D306; baz

$ he --encode --use-named-refs &#39;f&#246;o ♥ b&#229;r &#119558; baz&#39;
&gt; f&amp;ouml;o &amp;hearts; b&amp;aring;r &amp;#x1D306; baz

$ he --decode &#39;f&amp;ouml;o &amp;hearts; b&amp;aring;r &amp;#x1D306; baz&#39;
&gt; f&#246;o ♥ b&#229;r &#119558; 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(&#39;foo &amp;copy; bar &amp;ne; baz &amp;#x1D306; qux&#39;);
// → &#39;foo &#169; bar ≠ baz &#119558; qux&#39;

huangapple
  • 本文由 发表于 2020年1月6日 23:40:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614943.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定