简体   繁体   中英

How to get variable and pass value to API using JSON.stringify

I need to pass these values to an API using JSON.stringify and I need to get the variables in the object. The object that the API receives needs to look exactly like this:

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

I'm trying this, but I'm not sure exactly how to concatenate the variables in that context

const email = some.value,
const name = some.value,
const text = some.value,

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

How can I solve this? Thanks

You need to stringify internal object as well

 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM