[英]How to create the following type of json array using javascript?
How to create the following type of json array using javascript? 如何使用javascript创建以下类型的json数组?
xAxis: {
categories: [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec'
]
}
Well, you have two options: 嗯,你有两个选择:
Create the array and then stringify it: 创建数组然后将其字符串化:
var categories = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ]; var json = JSON.stringify(categories);
JSON.stringify
exists on most modern browsers, and you can shim it. JSON.stringify
存在于大多数现代浏览器中,您可以对其进行填充。 (There are several shims available, not least from Crockford's github page -- Crockford being the person who defined JSON.) (有几个垫片可用,尤其是来自Crockford的github页面 - Crockford是定义JSON的人。)
Or just create the JSON string directly: 或者直接创建JSON字符串:
var json = '["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]';
Re your edit: That's not "an array" anymore, it's an object with an array in it (or an object with an object in it with an array in that ). 回复您的编辑:这不是“数组”了,这是在一个数组(或在它的对象在一个数组的对象)的对象。 It doesn't fundmentally change the answer, though:
但它并没有根本改变答案:
var xAxis = { categories: [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ] };
var json = JSON.stringify(xAxis);
or 要么
var json = '{"categories": ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]}';
I wasn't sure whether you wanted the xAxis
layer in there. 我不确定你是否想要那里的
xAxis
层。 If so, it's just another layer around the above, eg: 如果是这样,它只是上面的另一层,例如:
var obj = { xAxis: { categories: [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ] } };
var json = JSON.stringify(obj);
or 要么
var json = '{"xAxis": {"categories": ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]}}';
More about JSON on the JSON home page . 有关JSON主页上的JSON的更多信息。 Fundamentally, all strings must be in double (not single) quotes, and all property names must be in double quotes.
从根本上说,所有字符串必须是双引号(非单引号),并且所有属性名称必须是双引号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.