繁体   English   中英

如何使用jquery从Javascript中提取特定数据字段?

[英]how to extract specific data field from Javascript using jquery?

我不确定是否已经问过这个问题,并且对jquery和javascript不熟悉。 但是,我找不到与我的问题有关的任何答案。 我正在尝试创建一个适用的抓取工具。 我需要从以下javascript中提取特定数据

<script type="application/json" class="js-react-on-rails-component">
{
    "assetHost": null,
    "version": "0.0.4-855-gda76bc6\n",
    "availableLocales": [
      "de",
      "en"
    ],
    .........
    "stats": {
        "visitors": [
          {
            "domestic": 600,
            "note": "incl. 250 conference participants",
            "year": 2017,
            "total": 600,
            "structure": null,
            "latest": true
          }
        ],
        "venue": [
          {
            "total": 376,
            "domestic": 376,
            "latest": true,
            "year": 2017
          }
        ],
        "exhibitors": [
          {
            "total_indirect": 0,
            "total": 46,
            "domestic": 46,
            "latest": true,
            "year": 2017
          }
        ]
      },

      ..........
      </script>

我需要获取字段:使用jquery从exhibitors那里获得totaldomestic信息。 我尝试了这个查询(JSON.parse($('.js-react-on-rails-component').text())).exhibitors.total.text().trim()但它没有返回任何内容。 因此,我尝试创建一个变量,并按如下所示对结果进行调用:

function pageFunction(context) {
    var $ = context.jQuery;
    var exhibitor = JSON.parse($('.js-react-on-rails-component').text());
    var total = exhibitor.exhibitors.total;
    var domestic = exhibitor.exhibitors.domestic;

    if (context.request.label === "START") {
    .....
    } else { 
    var result = {
            total: total,
            domestic: domestic
        };
    return result;  
    }
}

但是,此代码也不会返回任何结果。

既然你习惯了

(JSON.parse($( 'JS-反应-上导轨组分')。文本()))。exhibitors.total.text()。修剪()

如果您仔细查看json,则json的Exhibitions属性是一个数组,您必须使用Exhibitions [i] .totalExhibitionors [i] .domestic ,其中i = 0 ... N; 而不是您正在使用Exhibitions.total和Exhibitions.domestic

首先,为简单起见,请将dom组件带到一个变量中,

var myJson = JSON.parse($('.js-react-on-rails-component').text());

var total = myJson.stats.exhibitors[i].total;
var domestic = myJson.stats.exhibitors[i].domestic; 

其中i = 0 ... N;

编辑:

请看下面的例子

 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> </head> <body> <div></div> <script src="https://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script> <script type="application/json" id="myjsonscript"> { "stats": { "visitors": [ { "domestic": 600, "note": "incl. 250 conference participants", "year": 2017, "total": 600, "structure": null, "latest": true } ], "venue": [ { "total": 376, "domestic": 376, "latest": true, "year": 2017 } ], "exhibitors": [ { "total_indirect": 0, "total": 46, "domestic": 46, "latest": true, "year": 2017 } ] } } </script> <script id='script' type='text/javascript'> var myJson = JSON.parse($('#myjsonscript').text()); $('div').html(myJson); var total = myJson.stats.exhibitors[0].total; var domestic = myJson.stats.exhibitors[0].domestic; alert('total:' + total); alert('domestic: ' + domestic); </script> </body> </html> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM