[英]Assign array to JSON object as a new object
I have an array which includes some values, ex.我有一个包含一些值的数组,例如。
let array = [value1, value2];
How can I create a JSON object like the following?如何创建如下所示的 JSON 对象?
{
"field": "[value1, value2]"
}
JSON.stringify
for output a json, ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify JSON.stringify
用于输出 json,参考: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
I think you could find it pretty easily with a Google search :)我认为您可以通过 Google 搜索轻松找到它:)
const result = JSON.stringify({ field: ["toto", "tutu"]}) console.log(result)
Use JSON.stringify()
method:使用
JSON.stringify()
方法:
let array = [value1, value2];
let yourJObject={"field":JSON.stringify(array)}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.