简体   繁体   English

将数组作为新对象分配给 JSON 对象

[英]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.

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