需要幫助解析json文件。我需要從下面的文件中提取'選擇'。Json解析nodejs
{"questions":[
{"question1": "Who is Prime Minister of the United Kingdom?", "choices": ["David Cameron", "Gordon Brown", "Winston Churchill", "Tony Blair"], "correctAnswer":0},
{"question": "North West", "choices": ["What is the name of Kim Kardashian's baby?", "What is the opposite of south?"], "correctAnswer":0},
{"question": "What's my favorite color?", "choices": ["Black", "Blue", "Magenta", "Red"], "correctAnswer":1},
{"question": "What's the meaning of life?", "choices": ["Too live happily", "To give to the greater good"], "correctAnswer":1}
]}
腳本的NodeJS:
var fs = require("fs");
fs.readFile(__dirname + "/lib/questions.json", "Utf-8", function(err, data){
jsoncontent = JSON.parse(data);
//console.log(jsoncontent);
for (var i = 0; i < jsoncontent.length; ++i) {
//code
}
});
如何提取?
中示出預期結果來定義'extract'。同時顯示您用來自己解決這個問題的代碼。這不是一個代碼寫入服務,你需要顯示你的嘗試。 – charlietfl