In my project i am using highcart for that i should give the input in below format.
在我的项目中,我使用了highcart,我应该以下面的格式给出输入。
Here is my sample data:
这是我的样本数据:
[{"A",23},{"B",24},{"C",25},{"D",26},{"E",27}]
I have to convert the above json array to below format.
我必须将上面的json数组转换为下面的格式。
data=[["A",23],["B",24],["C",25],["D",26],["E",27]]
How to iterate my sample data and convert the above format. Any help will be greatly appreciated!!!
如何迭代示例数据并转换上面的格式。如有任何帮助,我们将不胜感激!!
2 个解决方案
#1
0
[{"A",23},{"B",24},{"C",25},{"D",26},{"E",27}]
is wrong,
it should be like [{"A":23},{"B":24},{"C":25},{"D":26},{"E":27}];
[{“A”,23 },{“B”,24 },{“C”,25 },{“D”,26日},{“E”,27 })是错误的,它应该像[{“A”:23 },{“B”:24 },{“C”:25 },{“D”:26 },{“E”:27 });
var data = [{"A":23},{"B":24},{"C":25},{"D":26},{"E":27}];
var newData = [];
data.forEach(function(val,index){
var keys = Object.keys(val);
keys.push(val[keys]);
newData.push(keys);
});
alert(JSON.stringify(newData));
#2
0
As your JSON format is wrong, you cannot use JSON.parse
on it directly. However, you can use regex
to get the data in the intended format.
由于JSON格式错误,所以不能使用JSON。它直接解析。但是,您可以使用regex以预期的格式获取数据。
Steps
步骤
- Replace all
{
by[
and}
by]
, you'll get the string that looks like nested arrays - 替换所有的{by [and} by],您将得到看起来像嵌套数组的字符串
- Convert the string to JSON using
JSON.parse()
to get the array from string - 使用JSON.parse()将字符串转换为JSON,从字符串中获取数组
var arr = '[{"A",23},{"B",24},{"C",25},{"D",26},{"E",27}]';
arr = JSON.parse(arr.replace(/\{/g, '[').replace(/\}/g, ']'));
console.log(arr);
console.log(arr[0][1]); // 23
#1
0
[{"A",23},{"B",24},{"C",25},{"D",26},{"E",27}]
is wrong,
it should be like [{"A":23},{"B":24},{"C":25},{"D":26},{"E":27}];
[{“A”,23 },{“B”,24 },{“C”,25 },{“D”,26日},{“E”,27 })是错误的,它应该像[{“A”:23 },{“B”:24 },{“C”:25 },{“D”:26 },{“E”:27 });
var data = [{"A":23},{"B":24},{"C":25},{"D":26},{"E":27}];
var newData = [];
data.forEach(function(val,index){
var keys = Object.keys(val);
keys.push(val[keys]);
newData.push(keys);
});
alert(JSON.stringify(newData));
#2
0
As your JSON format is wrong, you cannot use JSON.parse
on it directly. However, you can use regex
to get the data in the intended format.
由于JSON格式错误,所以不能使用JSON。它直接解析。但是,您可以使用regex以预期的格式获取数据。
Steps
步骤
- Replace all
{
by[
and}
by]
, you'll get the string that looks like nested arrays - 替换所有的{by [and} by],您将得到看起来像嵌套数组的字符串
- Convert the string to JSON using
JSON.parse()
to get the array from string - 使用JSON.parse()将字符串转换为JSON,从字符串中获取数组
var arr = '[{"A",23},{"B",24},{"C",25},{"D",26},{"E",27}]';
arr = JSON.parse(arr.replace(/\{/g, '[').replace(/\}/g, ']'));
console.log(arr);
console.log(arr[0][1]); // 23