从 JSON 对象中的非结构化字符串中提取 html 标签和数据 [英] Extract html tags and data from unstructured string within JSON object

查看:77
本文介绍了从 JSON 对象中的非结构化字符串中提取 html 标签和数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 NodeJS 和 Express 来调用 Eventbrite 的网络服务.从以下 JSON 对象中,我需要提取 img 标签和相关数据(src、宽度、高度)发生时.

I'm using NodeJS and Express to call Eventbrite's web service. From the following JSON object, I need to extract img tags and associated data (src, width, height) when they occur.

[ { name: 'Sense 5K - Austin Pre-Registration',
    url: 'http://austinpreregister.eventbrite.com/?aff=SRCH',
    start: '2014-10-01 09:00:00',
    description: '<P><IMG STYLE="display: block; margin-left: auto; margin-right: auto;" SRC="https://evbdn.eventbrite.com/s3-s3/eventlogos/90039995/about.png" ALT="" WIDTH="568" HEIGHT="138"></P>\r\n<P><IMG STYLE="vertical-align: middle; display: block; margin-left: auto; margin-right: auto;" SRC="https://evbdn.eventbrite.com/s3-s3/eventlogos/90039995/bluedawntour1.jpg" ALT="" WIDTH="568" HEIGHT="227"></P>',
    venue: 
     { city: 'Austin',
       name: '',
       country: 'United States',
       region: 'TX',
       longitude: -76.956325,
       postal_code: '',
       address_2: '',
       address: '',
       latitude: 38.861568,
       country_code: 'US',
       id: 5936809,
       'Lat-Long': '38.861568 / -76.956325' },
    organizer: 
     { url: 'http://www.eventbrite.com/o/sense-5k-6113542315',
       description: '\r\nAlong this enchanting journey you will encounter five Sense Scenes--each crafted to trigger an individual sense.The event is crowned with Sense Live, an unforgettable show and after party featuring pyrotechnics, dazzling special effects, a professional EDM DJ, live acrobatic entertainment, heart-pounding music, and the rhythm of your dancing. The purpose of Sense 5K is not to compete, but to experience! Every Sense 5K event benefits a local charity.\r\n \r\n',
       long_description: '\r\nAlong this enchanting journey you will encounter five Sense Scenes--each crafted to trigger an individual sense.The event is crowned with Sense Live, an unforgettable show and after party featuring pyrotechnics, dazzling special effects, a professional EDM DJ, live acrobatic entertainment, heart-pounding music, and the rhythm of your dancing. The purpose of Sense 5K is not to compete, but to experience! Every Sense 5K event benefits a local charity.\r\n ',
       id: 6113542315,
       name: 'Sense 5K' } } ]

来自这个 API 的响应各不相同(有时不包含图像,有时只包含图像,有时包含其他 html 中的图像),所以我正在寻找一种干净的方法来运行整个 JSON 对象,查找 img 标签,以及当它们出现时处理它们,不管它们出现在对象中的哪里.

The responses from this API vary (sometimes contains no image, sometimes contains image alone, sometimes contains image within other html), so I am looking for a clean way to run across the entire JSON object, look for img tags, and deal with them when they occur, regardless of where they occur within the object.

推荐答案

使用以下 JavaScript 和 jQuery:

Use the following JavaScript and jQuery:

var returnedObject = JSON.parse(returnedJSON);
$.each(returnedObject, function(index, value) {
    var $element = $(value.description);
    var images = $element.find('img');
    $.each(images, function(index, value) {
        console.log(value);
    });
});

这篇关于从 JSON 对象中的非结构化字符串中提取 html 标签和数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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