从JSON对象中的非结构化字符串中提取html标记和数据

时间:2014-08-26 23:29:22

标签: html json node.js web-services parsing

我正在使用NodeJS和Express来调用Eventbrite的Web服务。从下面的JSON对象中,我需要在它们出现时提取img标签和相关数据(src,width,height)。

[ { 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标记,以及在它们发生时处理它们,无论它们在对象中发生哪里

1 个答案:

答案 0 :(得分:0)

使用以下JavaScript和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);
    });
});