AngularJS纯文本解析带有一些简单的规则

时间:2016-03-28 08:21:33

标签: javascript angularjs json parsing plaintext

我有一个包含一些数据的.txt文件。我使用角度$http服务获取数据。但问题是数据不是json格式,而是有一些基本规则,我可以将其转换为json

这是一个数据示例:(我只写第一行,其他行与第一行类似)

有12个引号包含一些数据。

"123456789","some data","some data","some data","some data","some data","some data","some data","some data","some data","some data","some data"

在上面一行之后,一个新行以类似的结构开始。

如何解析它为json等数据交换格式?请参阅类似的question and answer

2 个答案:

答案 0 :(得分:3)

您可以使用split将其转换为值数组。

var text = '"123456789","some data","some data","some data","some data","some data","some data","some data","some data","some data","some data","some data"';

text = text.split(',');
console.log(text);

JSFiddle

答案 1 :(得分:2)

另一种方法是使用JSON.parse()

var elements = JSON.parse('["123456789","some data","some data","some data","some data","some data","some data","some data","some data","some data","some data","some data"]');
console.dir(elements);

这样做的好处是它能正确解析包含逗号","的值。