首页 > 解决方案 > 如何在 Javascript 中使用变量创建 JSON?

问题描述

我有返回 json 格式数组的代码,但我希望它位于“数据”下:所以现在,它显示为:

"["newDate": 'test'": 'test',"attendees": 'test,"name": 'Foo']"

我如何得到它: "{ data: ["newDate": 'test'": 'test',"attendees": 'test,"name": 'Foo'] }"

let countries = [];
    for (x in newObj) {
        countries.push({
            "newDate": newObj[x][0],
            "count": getDates()[x].length,
            "attendees": getDates()[x],
            "name": x
        })
    }
return JSON.stringify(countries);

标签: javascript

解决方案


JSON.stringify({data:countries});

推荐阅读