首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在javascript中过滤json?

如何在javascript中过滤json?
EN

Stack Overflow用户
提问于 2018-10-16 07:37:11
回答 1查看 36关注 0票数 -1

好了,我的代码从websocket中检索json,并且当前将所有json打印到控制台。我想要做的是将信息片段存储为变量。我怎么才能转身:

代码语言:javascript
复制
{
    "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"
        }
    ]

进入:

代码语言:javascript
复制
    Question = In wrestling, what term refers to pretending that scripted theatrics are totally real?
pAnswer1 = Gas
pAnswer2 = Kayfabe
pAnswer3 = House show

我正在使用node。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-16 07:49:04

这将提供您想要的内容。单击下面的"Run code snippet“查看输出。

代码语言:javascript
复制
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);    
})

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52826080

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档