簡體   English   中英

字符串解析到JavaScript中的對象轉換

[英]String parsing to object conversion in 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM