如何从JSON对象中提取多个主题标签?

时间:2017-08-20 11:40:35

标签: javascript json

我正在尝试从下面的JSON对象中提取“animal”和“fish”主题标签。我知道如何提取名为“animal”的第一个实例,但我不知道如何提取这两个实例。我在考虑使用循环,但不确定从哪里开始。请指教。

data = '{"hashtags":[{"text":"animal","indices":[5110,1521]},
        {"text":"Fish","indices":[122,142]}],"symbols":[],"user_mentions":
        [{"screen_name":"test241","name":"Test 
        Dude","id":4999095,"id_str":"489996095","indices":[30,1111]},
        {"screen_name":"test","name":"test","id":11999991,
        "id_str":"1999990", "indices":[11,11]}],"urls":[]}'; 

function showHashtag(data){
   i = 0;
   obj = JSON.parse(data); 
   console.log(obj.hashtags[i].text);
}

showHashtag(data);

3 个答案:

答案 0 :(得分:4)

使用Array.prototype.filter()



let data = '{"hashtags":[{"text":"animal","indices":[5110,1521]},{"text":"Fish","indices":[122,142]}],"symbols":[],"user_mentions":[{"screen_name":"test241","name":"Test Dude","id":4999095,"id_str":"489996095","indices":[30,1111]},   {"screen_name":"test","name":"test","id":11999991,        "id_str":"1999990", "indices":[11,11]}],"urls":[]}';

function showHashtag(data){
    return JSON.parse(data).hashtags.filter(e => /animal|fish/i.test(e.text))
}

console.log(showHashtag(data));




为了使函数可重用,如果你想找到其他" hashtags",你可以传递一个这样的数组:

function showHashtag(data, tags){
    let r = new RegExp(tags.join("|"), "i");
    return JSON.parse(data).hashtags.filter(e => r.test(e.text))
}

console.log(showHashtag(data, ['animal', 'fish']));

要获取text属性,只需链映射()

console.log(showHashtag(data, ['animal', 'fish']).map(e => e.text));

或在函数中

return JSON.parse(data).hashtags
    .filter(e => /animal|fish/i.test(e.text))
    .map(e => e.text);

编辑:

如果你想要的只是一个['animal', 'fish']的数组,我真的不明白为什么要用动物和鱼过滤。要仅获取具有text属性的对象,请再次使用filter,但是像这样



let data = '{"hashtags":[{"text":"animal","indices":[5110,1521]},{"text":"Fish","indices":[122,142]}],"symbols":[],"user_mentions":[{"screen_name":"test241","name":"Test Dude","id":4999095,"id_str":"489996095","indices":[30,1111]},   {"screen_name":"test","name":"test","id":11999991,        "id_str":"1999990", "indices":[11,11]}],"urls":[]}';

function showHashtag(data){
    return JSON.parse(data).hashtags
      .filter(e => e.text)
      .map(e => e.text);
}

console.log(showHashtag(data));




答案 1 :(得分:1)

对我来说,Lodash在这里很有用,它在集合方面有不同的功能。对于你的情况,我使用_.find函数来帮助检查数组,并获得任何带有作为第二个参数传入的creteria的标记,如下所示:

  

.find(collection,[predicate = .identity],[fromIndex = 0])   来源npm包

     

迭代集合的元素,返回第一个元素   谓词返回truthy for。谓词用三个调用   参数:(value,index | key,collection)。

根据你的情况,这应该有效

var data = '{ "hashtags": [ { "text": "animal", "indices": [ 5110, 1521 ] }, { "text": "Fish", "indices": [ 122, 142 ] } ], "symbols": [], "user_mentions": [ { "screen_name": "test241", "name": "Test \n Dude", "id": 4999095, "id_str": "489996095", "indices": [ 30, 1111 ] }, { "screen_name": "test", "name": "test", "id": 11999991, "id_str": "1999990", "indices": [ 11, 11 ] } ], "urls": [] }'; 

var obj = JSON.parse(data); 


_.find(obj.hashtags, { 'text': 'animal' });
// => { "text": "animal", "indices": [ 5110, 1521 ] }

答案 2 :(得分:1)

对于像这样的简单解析,我会使用普通的B方法,它更易读,更容易理解,特别是对于javascript初学者。

obj.forEach()