JavaScript 中的字符串解析到对象转换

玛撒拉

鉴于以下 html 源代码是一个字符串,我将如何解析它以提取var dataObject并将其作为具有属性的对象返回给用户?

<!doctype html>
<!--[if IE 7]><html class="no-js lt-ie10 lt-ie9 lt-ie8"><![endif]-->
<!--[if IE 8]><html class="no-js lt-ie10 lt-ie9"><![endif]-->
<!--[if IE 9]><html class="no-js lt-ie10"><![endif]-->
<!--[if gt IE 9]><!-->
<html class="no-js">
<!--<![endif]-->

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>Air Retro 15 Obsidian - Footpatrol | Footpatrol</title>
    <meta name="description" content="" />
    <meta name="keywords" content="" />
    <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, maximum-scale=1.0, target-densityDpi=device-dpi" />
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-title" content="Footpatrol - TEAM FP">
    <meta name="format-detection" content="telephone=no">
    <link rel="shortcut icon" href="/templates/footpatrol.co.uk/_assets/icons/favicon.ico" type="image/x-icon" />
    <link rel="apple-touch-icon" sizes="57x57" href="/templates/footpatrol.co.uk/_assets/icons/apple-touch-icon-57x57.png">
    <link rel="apple-touch-icon" sizes="60x60" href="/templates/footpatrol.co.uk/_assets/icons/apple-touch-icon-60x60.png">
    <link rel="apple-touch-icon" sizes="72x72" href="/templates/footpatrol.co.uk/_assets/icons/apple-touch-icon-72x72.png">
    <link rel="apple-touch-icon" sizes="76x76" href="/templates/footpatrol.co.uk/_assets/icons/apple-touch-icon-76x76.png">
    <link rel="apple-touch-icon" sizes="114x114" href="/templates/footpatrol.co.uk/_assets/icons/apple-touch-icon-114x114.png">
    <link rel="apple-touch-icon" sizes="120x120" href="/templates/footpatrol.co.uk/_assets/icons/apple-touch-icon-120x120.png">
    <link rel="apple-touch-icon" sizes="144x144" href="/templates/footpatrol.co.uk/_assets/icons/apple-touch-icon-144x144.png">
    <link rel="apple-touch-icon" sizes="152x152" href="/templates/footpatrol.co.uk/_assets/icons/apple-touch-icon-152x152.png">
    <link rel="apple-touch-icon" sizes="180x180" href="/templates/footpatrol.co.uk/_assets/icons/apple-touch-icon-180x180.png">
    <link rel="apple-touch-icon" sizes="57x57" href="/templates/footpatrol.co.uk/_assets/icons/apple-touch-icon-57x57-precomposed.png">
    <link rel="apple-touch-icon" sizes="60x60" href="/templates/footpatrol.co.uk/_assets/icons/apple-touch-icon-60x60-precomposed.png">
    <link rel="apple-touch-icon" sizes="72x72" href="/templates/footpatrol.co.uk/_assets/icons/apple-touch-icon-72x72-precomposed.png">
    <link rel="apple-touch-icon" sizes="76x76" href="/templates/footpatrol.co.uk/_assets/icons/apple-touch-icon-76x76-precomposed.png">
    <link rel="apple-touch-icon" sizes="114x114" href="/templates/footpatrol.co.uk/_assets/icons/apple-touch-icon-114x114-precomposed.png">
    <link rel="apple-touch-icon" sizes="120x120" href="/templates/footpatrol.co.uk/_assets/icons/apple-touch-icon-120x120-precomposed.png">
    <link rel="apple-touch-icon" sizes="144x144" href="/templates/footpatrol.co.uk/_assets/icons/apple-touch-icon-144x144-precomposed.png">
    <link rel="apple-touch-icon" sizes="152x152" href="/templates/footpatrol.co.uk/_assets/icons/apple-touch-icon-152x152-precomposed.png">
    <link rel="apple-touch-icon" sizes="180x180" href="/templates/footpatrol.co.uk/_assets/icons/apple-touch-icon-180x180-precomposed.png">
    <link rel="icon" sizes="192x192" href="/templates/footpatrol.co.uk/_assets/icons/touch-icon-192x192.png">
    <link rel="icon" type="image/png" href="/templates/footpatrol.co.uk/_assets/icons/favicon-16x16.png" sizes="16x16">
    <link rel="icon" type="image/png" href="/templates/footpatrol.co.uk/_assets/icons/favicon-32x32.png" sizes="32x32">
    <link rel="icon" type="image/png" href="/templates/footpatrol.co.uk/_assets/icons/favicon-96x96.png" sizes="96x96">
    <link rel="icon" type="image/png" href="/templates/footpatrol.co.uk/_assets/icons/favicon-160x160.png" sizes="160x160">
    <link rel="icon" type="image/png" href="/templates/footpatrol.co.uk/_assets/icons/favicon-192x192.png" sizes="192x192">
    <link href="/templates/footpatrol.co.uk/_assets/icons/apple-touch-startup-image-1536x2008.png" media="(device-width: 768px) and (device-height: 1024px) and (-webkit-device-pixel-ratio: 2) and (orientation: portrait)" rel="apple-touch-startup-image">
    <link href="/templates/footpatrol.co.uk/_assets/icons/apple-touch-startup-image-1496x2048.png" media="(device-width: 768px) and (device-height: 1024px) and (-webkit-device-pixel-ratio: 2) and (orientation: landscape)" rel="apple-touch-startup-image">
    <link href="/templates/footpatrol.co.uk/_assets/icons/apple-touch-startup-image-768x1004.png" media="(device-width: 768px) and (device-height: 1024px) and (-webkit-device-pixel-ratio: 1) and (orientation: portrait)" rel="apple-touch-startup-image">
    <link href="/templates/footpatrol.co.uk/_assets/icons/apple-touch-startup-image-748x1024.png" media="(device-width: 768px) and (device-height: 1024px) and (-webkit-device-pixel-ratio: 1) and (orientation: landscape)" rel="apple-touch-startup-image">
    <link href="/templates/footpatrol.co.uk/_assets/icons/apple-touch-startup-image-1242x2148.png" media="(device-width: 414px) and (device-height: 736px) and (-webkit-device-pixel-ratio: 3) and (orientation: portrait)" rel="apple-touch-startup-image">
    <link href="/templates/footpatrol.co.uk/_assets/icons/apple-touch-startup-image-1182x2208.png" media="(device-width: 414px) and (device-height: 736px) and (-webkit-device-pixel-ratio: 3) and (orientation: landscape)" rel="apple-touch-startup-image">
    <link href="/templates/footpatrol.co.uk/_assets/icons/apple-touch-startup-image-750x1294.png" media="(device-width: 375px) and (device-height: 667px) and (-webkit-device-pixel-ratio: 2)" rel="apple-touch-startup-image">
    <link href="/templates/footpatrol.co.uk/_assets/icons/apple-touch-startup-image-640x1096.png" media="(device-width: 320px) and (device-height: 568px) and (-webkit-device-pixel-ratio: 2)" rel="apple-touch-startup-image">
    <link href="/templates/footpatrol.co.uk/_assets/icons/apple-touch-startup-image-640x920.png" media="(device-width: 320px) and (device-height: 480px) and (-webkit-device-pixel-ratio: 2)" rel="apple-touch-startup-image">
    <link href="/templates/footpatrol.co.uk/_assets/icons/apple-touch-startup-image-320x460.png" media="(device-width: 320px) and (device-height: 480px) and (-webkit-device-pixel-ratio: 1)" rel="apple-touch-startup-image">
    <meta name="google-site-verification" content="zzI3fd6H_JByuEoUy-mwtsEdSARxSXpjoSDKWLFOAYA" />
    <meta property="og:title" content="Air Retro 15 Obsidian" />
    <meta property="og:type" content="product" />
    <meta property="og:url" content="http://www.footpatrol.co.uk/footwear/282524-air-retro-15-obsidian.html" />
    <meta property="og:image" content="http://www.footpatrol.co.uk/images/ac_product_images/product_image_data/resizeandpad:324:488/http://i1.adis.ws/i/jpl/fp_282524_a.jpg" />
    <meta property="og:site_name" content="Footpatrol" />
    <meta property="fb:admins" content="501344983" />
    <!-- !CSS -->
    <link href="/templates/footpatrol.co.uk/_css/swiper.css?v=47" rel="stylesheet" type="text/css" />
    <link href="/templates/footpatrol.co.uk/_css/master.css?v=47" rel="stylesheet" type="text/css" />
    <link href="/templates/footpatrol.co.uk/_css/fp.main.css?v=47" rel="stylesheet" type="text/css" />
    <link href="/templates/footpatrol.co.uk/_css/fp.mobile.css?v=47" rel="stylesheet" type="text/css" />
    <link href="/templates/footpatrol.co.uk/_css/fp.tablet.css?v=47" rel="stylesheet" type="text/css" />
    <link href="/templates/footpatrol.co.uk/_css/fp.master.overrides.css?v=47" rel="stylesheet" type="text/css" />
    <link href="http://fonts.googleapis.com/css?family=Lato:300,400,700" rel="stylesheet" type="text/css">
    <!-- !JS -->
    <script type="text/javascript">
        var dataObject = {

            brand: 'Jordan',
            currency: 'GBP',
            category: 'Footwear',
            platform: 'desktop',
            pageType: 'product',
            pageName: 'Air Retro 15 Obsidian - Footpatrol',
            custId: '',
            plu: '282524',
            description: 'Air Retro 15 Obsidian',
            unitPrice: '160',
            sale: 'false'

        };
    </script>

目前我正在使用以下技术方法,它并不总是以正确的格式返回字符串:

function getProductName(html) {
    var temp = document.createElement('div');
    temp.innerHTML = html;
    var name = temp.querySelector('h1[itemprop*=\'name\']').textContent.trim();
    $log.debug('Name found: ' + name);
    return name;
}
拉胡尔·阿罗拉

您可以按照以下示例进行操作:

var givenString = "<script>var dataObject={firstname: 'rahul'}</script>"

var doc = new DOMParser().parseFromString(givenString, 'text/html');

var scriptContent = doc.getElementsByTagName('script');

var dataObject = [].map.call(scriptContent, function(item) {
    return item.textContent;
});

//try this code to get the real object
var a = dataObject[0];
a += ';return dataObject;'; //dataObject is the name of the variable that contains what I want to return

var f = new Function(a);

console.log(f);

var object = f();

console.log(object); //will give you the object

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

字节数组到javascript中的十六进制字符串转换

Javascript字符串到int的转换

将JSON字符串解析为JavaScript中的特定对象原型

在JavaScript中解析DateTime字符串

将字符串对象转换为Javascript中的字符串基元

如何解析嵌入在JavaScript对象内部的字符串中的html?

在javascript中映射对象的字符串

将字符串转换为Javascript中的对象

字符串到数字的转换无法在javascript中给出预期的结果

Javascript-在默认解析之前将EXTRA LARGE Number转换为JSON中的字符串

转换对象中的JavaScript点字符串

Javascript-解析JSON寻找对象字符串

将对象转换为javascript中的字符串,而键中没有引号

JavaScript中的字符串转换

JavaScript中十进制的字符串转换(十进制到字符串)

Javascript:解析字符串中的json对象数组

Javascript数组到字符串的转换

将values属性转换为对象中javascript中的字符串

在JavaScript中解析和转换XML字符串

将字符串转换为Javascript中的JSON对象

通过javascript中的encodeURIComponent进行字符串转换时,将字符串解析为原始形式

将字符串转换为JavaScript中的对象数组

无法解析JavaScript字符串中的\

我如何通过 javascript 传递 mqtt 字符串来转换并插入到 mysql 中?

字符串到 JavaScript 中的对象,一个对象

Javascript从字符串中获取对象

javascript中字符串到html的转换

javascript中的字符串到数组对象

Javascript - 根据字典/对象中的键解析字符串