将HTML实体从JSON转换为字符串 [英] Converting HTML Entities from JSON to String

查看:581
本文介绍了将HTML实体从JSON转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用聚合物,但对它是新手(今天才开始使用它),在显示来自JSON的数据时遇到了一些麻烦,例如 ’&#8220等.

I'm using polymer and I'm new to it (just started using it today) and I have some troubles displaying data coming from JSON like  , ’, &#8220 etc.

HTML

<news-card>        
    <h1>{{summary.title}}</h1>
    <img src="{{summary.thumbnail}}"></img>
    <span>{{summary.published}}</span>
    <p>{{summary.summary}}</p>
</news-card>

例如JSON:

{
 title: '&#8217; This is a title',
 thumbnail: 'test.jpg',
 published: 'October 15'
 summary: '&#8220; &nbsp; &nbsp;'
}

例如输出:

&#8217; This is a title
October 15
&#8220; &nbsp; &nbsp;

推荐答案

我使用自定义过滤器来显示所需的输出.

I used custom filters to display the desired output.

HTML

<news-card>

    <h1>{{summary.title | encodeEntities}}</h1>
    <img src="{{summary.thumbnail}}"></img>
    <span>{{summary.published}}</span>

    <p>{{summary.summary | encodeEntities}}</p>
</news-card>

脚本

Polymer('your-polymer-element-name', {
      encodeEntities: function(value) {
        var div = document.createElement('div');
        div.innerHTML = value;
        return div.innerHTML;
      }
  });

来源:

聚合物-表达式:自定义过滤器

Stackoverflow-如何在Polymer元素定义中分配HTML实体?

这篇关于将HTML实体从JSON转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆