我有一个包含一些数据的.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。
答案 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);
答案 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);
这样做的好处是它能正确解析包含逗号","的值。