好了,我的代码从websocket中检索json,并且当前将所有json打印到控制台。我想要做的是将信息片段存储为变量。我怎么才能转身:
{
"type": "question",
"ts": "2018-06-30T00:05:53.685Z",
"totalTimeMs": 10000,
"timeLeftMs": 10000,
"questionId": 46220,
"question": "In wrestling, what term refers to pretending that scripted theatrics are totally real?",
"category": "Entertainment",
"answers": [{
"answerId": 140757,
"text": "Gas"
},
{
"answerId": 140758,
"text": "Kayfabe"
},
{
"answerId": 140759,
"text": "House show"
}
]
进入:
Question = In wrestling, what term refers to pretending that scripted theatrics are totally real?
pAnswer1 = Gas
pAnswer2 = Kayfabe
pAnswer3 = House show
我正在使用node。
发布于 2018-10-16 07:49:04
这将提供您想要的内容。单击下面的"Run code snippet“查看输出。
const obj = {
"type": "question",
"ts": "2018-06-30T00:05:53.685Z",
"totalTimeMs": 10000,
"timeLeftMs": 10000,
"questionId": 46220,
"question": "In wrestling, what term refers to pretending that scripted theatrics are totally real?",
"category": "Entertainment",
"answers": [{
"answerId": 140757,
"text": "Gas"
}, {
"answerId": 140758,
"text": "Kayfabe"
}, {
"answerId": 140759,
"text": "House show"
}]
}
const question = obj.question;
const pAnswer = obj.answers.map(ans => ans.text);
console.log('question =', question);
pAnswer.forEach((ans, i) => {
console.log(`answer${i+1} =`, ans);
})
https://stackoverflow.com/questions/52826080
复制相似问题