Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/446.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript中字符串解析到对象转换_Javascript - Fatal编程技术网

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

JavaScript中字符串解析到对象转换,javascript,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]-->

假设下面的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>

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

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
var givenString=“var dataObject={firstname:'rahul'}”
var doc=new DOMParser().parseFromString(givenString,'text/html');
var scriptContent=doc.getElementsByTagName('script');
var dataObject=[].map.call(脚本内容、函数(项){
返回item.textContent;
});
//尝试此代码以获取真实对象
var a=数据对象[0];
a+=';返回数据对象;'//dataObject是包含我要返回的内容的变量的名称
var f=新函数(a);
控制台日志(f);
var object=f();
console.log(对象)//我会给你这个东西

var parser=new DOMParser(),htmlDoc=parser.parseFromString(html,“text/html”)。。。然后在
htmlDoc
Hi-Rahul上使用DOM技术,返回
var-dataObject={}作为字符串如何将其映射到对象,以便访问以下属性:
dataObject[0]。name
等。检查我的更新答案。做了一点小改动以获取对象的值。基本上,我将该字符串作为javascript代码执行,方法是用它生成一个函数并返回对象的值。本可以使用
eval
,但速度慢且不安全。注意:名称
对象
已硬编码,您可以在您的案例中处理。它不能按预期工作,请检查一下好吗?不确定
var a=dataString[0]发生了什么以及jslint在
var f=new Function(a)处抛出错误“函数构造函数是eval的一种形式”。在
var a=dataObject[0]处成功检索数据对象(必须进行更改),但在尝试将其映射到对象时似乎存在问题。要映射的数据对象的示例如下:
var dataObject={品牌:'Jordan',货币:'GBP',类别:'Shoots',平台:'desktop',页面类型:'product',页面名称:'Air Retro 15 Obsidian-Footatrol',custId:'plu:'282524',描述:'Air Retro 15 Obsidian',单价:'160',销售:'false';
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