2017-06-07 53 views
2

在JSON數據的數據我想獲取從JSON文件中的一些數據在Javascript發現在Javascript

我有這樣的JSON數據:

var bib = { 
"AnthesGarcia-HernandezEtAl2016": { 
    "author": "C. Anthes and R. J. Garcia-Hernandez and M. Wiedemann and D. Kranzlmuller", 
    "booktitle": "2016 IEEE Aerospace Conference", 
    "doi": "10.1109/AERO.2016.7500674", 
    "file": "AnthesGarcia-HernandezEtAl2016.pdf:AnthesGarcia-HernandezEtAl2016.pdf:PDF", 
    "keywords": "virtual reality, survey, aerospace", 
}, 
"Bastos2014": { 
    "author": "Bastos, Ricardo S", 
    "file": "Bastos2014.pdf:Bastos2014.pdf:PDF", 
    "institution": "DTIC Document", 
    "keywords": "maritime, military, submarine, training, simulator, virtual reality, web, webgl, navy" 

}, 
"Baum1994": { 
    "author": "Baum, David R", 
    "keywords": "patent, virtual reality, gestures, aviation, training, head-mounted-display, HMD, gloves", 

} 

我怎麼能取這樣的數據在JS:

var keywords = { 
    "keywords-name": { // keywords-name as example : virtual reality 
     "repeat": "??" 

    }, 
    "keywords-name2": { 
     "repeat": "??" 
     }, 
    "keywords-name3": { 
     "repeat": "??" 
}, ... 

我不知道如何在JavaScript做..

+0

目前還不清楚你想要什麼。你想輸出什麼? – evolutionxbox

回答

0

使用

,你可以做這樣的:

var bib = { 
 
    "AnthesGarcia-HernandezEtAl2016": { 
 
    "author": "C. Anthes and R. J. Garcia-Hernandez and M. Wiedemann and D. Kranzlmuller", 
 
    "booktitle": "2016 IEEE Aerospace Conference", 
 
    "doi": "10.1109/AERO.2016.7500674", 
 
    "file": "AnthesGarcia-HernandezEtAl2016.pdf:AnthesGarcia-HernandezEtAl2016.pdf:PDF", 
 
    "keywords": "virtual reality, survey, aerospace", 
 
    }, 
 
    "Bastos2014": { 
 
    "author": "Bastos, Ricardo S", 
 
    "file": "Bastos2014.pdf:Bastos2014.pdf:PDF", 
 
    "institution": "DTIC Document", 
 
    "keywords": "maritime, military, submarine, training, simulator, virtual reality, web, webgl, navy" 
 
    }, 
 
    "Baum1994": { 
 
    "author": "Baum, David R", 
 
    "keywords": "patent, virtual reality, gestures, aviation, training, head-mounted-display, HMD, gloves" 
 
    } 
 
}; 
 

 
var keywords = Object.keys(bib).reduce((a, v) => { 
 
    bib[v].keywords.split(/\s*,\s*/).forEach(kw => { 
 
    a[kw] = a[kw] || {repeat: 0}; 
 
    a[kw].repeat++; 
 
    }); 
 

 
    return a; 
 
}, {}); 
 

 
console.log(keywords)

+0

tnx這麼多。但我如何定製你的代碼,看到這樣的輸出: 節點:[{id:「virtual reality」,repeate:3},{id:「survey」,repeat:1} – vahidsamimi

+0

我寫了這段代碼,無法正常工作: var res = { \t nodes:[] }; (var key in repeats){ if(repeats.hasOwnProperty(key)){ var temp = repeats [key]; temp [「id」] = key; res.nodes.push(temp); } } – vahidsamimi

-1

ü可以試試這個:

alert(keywords.keywords-name) 

應該得到的值。

+0

它不僅不回答問題,甚至不會執行。 –

+0

對不起,我在看到@RobbyCornelissen答案後誤解了這個問題。 Thx指出我的錯誤。 –