如何从json文件读取并使用DOM值并搜索json文件包含该值

时间:2018-11-11 21:34:52

标签: javascript json dom search

我使用defiantjs 2.0.4,我想搜索json文件是否包含值。我从此DOM中获得了价值

var value = document.getElementById('input').value      

但随后的搜索未在日志输出中返回正确的值。

 var res = JSON.search( json, '//*[contains(title,' + value + ')]');

以上代码返回所有带undefined的无效搜索,我搜索“ red” 它应该只返回红地毯,因为它存在于以下json文件中

 //FIXME 
var json = JSON.parse($.getJSON({'url': "js/moviesj.json", 'async': false}).responseText);
     var value = document.getElementById('input').value;     
     //FIXME 
     var res = JSON.search( json, '//*[contains(title,' + value + ')]');
      var  strTitle = '';
     var   strUrl = '';
        for (var i=0; i<res.length; i++) {
            strTitle += res[i].title +'<br/>';
            strUrl += res[i].video_url +'<br/>';

        }
        console.log(res);
        console.log(strTitle);
        console.log(strUrl);

这是moviej.json文件:

[{"video_url": "http://63.237.48.3/ipnx/media/movies/Zane_Ziadi_HQ.mp4", "title": "Zane Ziadi"},
{"video_url": "http://63.237.48.3/ipnx/media/movies/DarBastAzadiHQ.mp4", "title": "Darbast Azadi"},
{"video_url": "http://63.237.48.3/ipnx/media/movies/Cheghadr_Vaght_Dari_HQ.mp4", "title": "Cheghadr Vaght Dari"},
{"video_url": "http://63.237.48.3/ipnx/media/movies/Mashaal_HQ.mp4", "title": "Mashaal"},
{"video_url": "http://63.237.48.3/ipnx/media/movies/Red_Carpet_HQ.mp4", "title": "Red Carpet"},
{"video_url": "http://63.237.48.3/ipnx/media/movies/Ba_Digaran_HQ.mp4", "title": "Ba Digaran"},
{"video_url": "http://63.237.48.3/ipnx/media/movies/Metro_Pol_HQ.mp4", "title": "Metropol"},
{"video_url": "http://63.237.48.3/ipnx/media/movies/Emrooz_HQ.mp4", "title": "Emrooz"},
{"video_url": "http://63.237.48.3/ipnx/media/movies/5Setareh.mp4", "title": "5 Setareh"}]

请让我现在使用defiantjs或替代库的上述代码缺少的内容

0 个答案:

没有答案