从javascript解析id

时间:2012-12-04 08:24:22

标签: javascript parsing

考虑到这个(示例),我有html代码:

$(window).ready(function () {
    video.search.apply_results({
        "scope":"global",
        "global":{
            "list":[{
                "id":19366,
                "user_id":null,
                "title":"\u041c\u043e\u0440\u0441\u043a\u0430\u044f \u0418\u0441\u0442\u043e\u0440\u0438\u044f: \u0422\u043e, \u0447\u0442\u043e \u0432 \u0442\u0432\u043e\u0438\u0445 \u0441\u0438\u043b\u0430\u0445 \/ Umi Monogatari: Anata ga Ite [hl]Kureta[\/hl] Koto (RUS) \u0421\u0435\u0440\u0438\u044f \u21161",
                "thumb_url":"http:\/\/cs1.premedias.ru\/videos\/1\/9\/3\/6\/6\/6982b758bb030f4075d40696433158ef_s.jpg",
                "duration":1453,
                "description":"\u0414\u0432\u0435 \u0441\u0435\u0441\u0442\u0440\u044b-\u0440\u0443\u0441\u0430\u043b\u043a\u0438 \u041c\u0430\u0440\u0438\u043d \u0438 \u0423\u0440\u0438\u043d \u0436\u0438\u0432\u0443\u0442 \u0432 \u043e\u043a\u0435\u0430\u043d\u0441\u043a\u0438\u0445 \u0433\u043b\u0443\u0431\u0438\u043d\u0430\u0445, \u043e\u0431\u0449\u0430\u044e\u0442\u0441\u044f \u0441 \u0440\u043e\u0434\u0438\u0447\u0430\u043c\u0438, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0441 \u0440\u044b\u0431\u0430\u043c\u0438, \u0447...",
                "is_hd":true
            },{
                "id":19367,
                "user_id":null,
                "title":"\u041c\u043e\u0440\u0441\u043a\u0430\u044f \u0418\u0441\u0442\u043e\u0440\u0438\u044f: \u0422\u043e, \u0447\u0442\u043e \u0432 \u0442\u0432\u043e\u0438\u0445 \u0441\u0438\u043b\u0430\u0445 \/ Umi Monogatari: Anata ga Ite [hl]Kureta[\/hl] Koto (RUS) \u0421\u0435\u0440\u0438\u044f \u21162",
                "thumb_url":"http:\/\/cs1.premedias.ru\/videos\/1\/9\/3\/6\/7\/d2dc60de1ec00f944e74b2aa1f6db942_s.jpg",
                "duration":1454,
                "description":"\u0414\u0432\u0435 \u0441\u0435\u0441\u0442\u0440\u044b-\u0440\u0443\u0441\u0430\u043b\u043a\u0438 \u041c\u0430\u0440\u0438\u043d \u0438 \u0423\u0440\u0438\u043d \u0436\u0438\u0432\u0443\u0442 \u0432 \u043e\u043a\u0435\u0430\u043d\u0441\u043a\u0438\u0445 \u0433\u043b\u0443\u0431\u0438\u043d\u0430\u0445, \u043e\u0431\u0449\u0430\u044e\u0442\u0441\u044f \u0441 \u0440\u043e\u0434\u0438\u0447\u0430\u043c\u0438, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0441 \u0440\u044b\u0431\u0430\u043c\u0438, \u0447...",
                "is_hd":true
            }],
            "count":28
        }
    });
});

我需要回显第一个“id”之后的数字:在这个例子中它是19366.我怎么能用javascript做呢?

4 个答案:

答案 0 :(得分:1)

假设您正在编写apply_results函数,它看起来有点像这样:

function apply_results(opts) {
    // ...
    // alerts first id
    alert(opts.global.list[0].id);
}

如果您只是将上面的html作为文本,并且您想从中提取id,请使用以下regexp:

// assuming text holds all the html OP pasted following alerts first id found
alert(text.match(/\"id\":(\d+)/)[1]);

答案 1 :(得分:1)

不需要(进一步)解析。一旦从任何来源获得该数据,基于您引用的代码,它已经被反序列化(解析)为对象图。您只需引用该对象图的global.list[0].id即可。例如:

$(window).ready(function () {
    var results = {
        "scope": "global",
        "global": {
            "list": [{
                "id": 19366,
                "user_id": null,
                "title": "\u041c\u043e\u0440\u0441\u043a\u0430\u044f \u0418\u0441\u0442\u043e\u0440\u0438\u044f: \u0422\u043e, \u0447\u0442\u043e \u0432 \u0442\u0432\u043e\u0438\u0445 \u0441\u0438\u043b\u0430\u0445 \/ Umi Monogatari: Anata ga Ite [hl]Kureta[\/hl] Koto (RUS) \u0421\u0435\u0440\u0438\u044f \u21161",
                "thumb_url": "http:\/\/cs1.premedias.ru\/videos\/1\/9\/3\/6\/6\/6982b758bb030f4075d40696433158ef_s.jpg",
                "duration": 1453,
                "description": "\u0414\u0432\u0435 \u0441\u0435\u0441\u0442\u0440\u044b-\u0440\u0443\u0441\u0430\u043b\u043a\u0438 \u041c\u0430\u0440\u0438\u043d \u0438 \u0423\u0440\u0438\u043d \u0436\u0438\u0432\u0443\u0442 \u0432 \u043e\u043a\u0435\u0430\u043d\u0441\u043a\u0438\u0445 \u0433\u043b\u0443\u0431\u0438\u043d\u0430\u0445, \u043e\u0431\u0449\u0430\u044e\u0442\u0441\u044f \u0441 \u0440\u043e\u0434\u0438\u0447\u0430\u043c\u0438, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0441 \u0440\u044b\u0431\u0430\u043c\u0438, \u0447...",
                "is_hd": true
            }, {
                "id": 19367,
                "user_id": null,
                "title": "\u041c\u043e\u0440\u0441\u043a\u0430\u044f \u0418\u0441\u0442\u043e\u0440\u0438\u044f: \u0422\u043e, \u0447\u0442\u043e \u0432 \u0442\u0432\u043e\u0438\u0445 \u0441\u0438\u043b\u0430\u0445 \/ Umi Monogatari: Anata ga Ite [hl]Kureta[\/hl] Koto (RUS) \u0421\u0435\u0440\u0438\u044f \u21162",
                "thumb_url": "http:\/\/cs1.premedias.ru\/videos\/1\/9\/3\/6\/7\/d2dc60de1ec00f944e74b2aa1f6db942_s.jpg",
                "duration": 1454,
                "description": "\u0414\u0432\u0435 \u0441\u0435\u0441\u0442\u0440\u044b-\u0440\u0443\u0441\u0430\u043b\u043a\u0438 \u041c\u0430\u0440\u0438\u043d \u0438 \u0423\u0440\u0438\u043d \u0436\u0438\u0432\u0443\u0442 \u0432 \u043e\u043a\u0435\u0430\u043d\u0441\u043a\u0438\u0445 \u0433\u043b\u0443\u0431\u0438\u043d\u0430\u0445, \u043e\u0431\u0449\u0430\u044e\u0442\u0441\u044f \u0441 \u0440\u043e\u0434\u0438\u0447\u0430\u043c\u0438, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0441 \u0440\u044b\u0431\u0430\u043c\u0438, \u0447...",
                "is_hd": true
            }],
            "count": 28
        }
    };
    video.search.apply_results(results);
    console.log(results.global.list[0].id);
});

我上面所做的只是将您传入apply_results的大量对象文字移动到变量(results),将该变量与apply_results一起使用,然后显示第一个{{ 1}}值(id)。并格式化代码。 : - )

答案 2 :(得分:1)

您可以使用数组投影方法.map()

var ids = json.global.list.map(function(i){
  return i.id;
});

console.log(ids);//[19366, 19367]

答案 3 :(得分:0)

我不确定你想要什么!顺便说一句,我想象过这样的情况:

<video id="id19367"/>
<video id="19367"/>​

但是,一旦找到了id,就可以使用replace()javascript函数来“删除”'id'。

$(document).ready(function(){
  var ids = new Array();
  $('video').each(function() {
     var id = $(this).attr('id');  
      if(id[0]=='i') {
          var toRemove = 'id';
          id = id.replace(toRemove,'');
      }
      ids.push(id);    
      console.log(id);          
  });
});​