如何使用JSON.stringify获取变量并将值传递给API



我需要使用JSON将这些值传递给API。我需要得到对象中的变量。API接收的对象需要完全像这样:

{
"sessionInformation": "{"emailAddress" : "the value","firstName" : "the name", "question" : "the text"}"
}

我正在尝试这个,但我不确定如何在该上下文中连接变量

const email = some.value,
const name = some.value,
const text = some.value,
const raw = JSON.stringify({
sessionInformation:
'{"emailAddress" : email,"firstName" : name, "question" : text}',
});

我该如何解决这个问题?由于

还需要对内部对象进行字符串化

const email = "Email value";
const name = "Name value";
const text = "Text value";
const raw = JSON.stringify({
sessionInformation: JSON.stringify({
emailAddress: email,
firstName: name,
question: text
}),
});
console.log(raw)

最新更新